diff --git a/platform/util/src/com/intellij/util/io/PersistentHashMap.java b/platform/util/src/com/intellij/util/io/PersistentHashMap.java index b5f0aa509f79..8cf3e40ed56c 100644 --- a/platform/util/src/com/intellij/util/io/PersistentHashMap.java +++ b/platform/util/src/com/intellij/util/io/PersistentHashMap.java @@ -32,7 +32,7 @@ import java.util.Collection; */ public class PersistentHashMap extends PersistentEnumerator{ private static final Logger LOG = Logger.getInstance("#com.intellij.util.io.PersistentHashMap"); - + private static final int APPEND_CACHE_DATA_THRESHOLD = 20 * 1024 * 1024; // 20 MB private PersistentHashMapValueStorage myValueStorage; private final DataExternalizer myValueExternalizer; private static final long NULL_ADDR = 0; @@ -50,6 +50,10 @@ public class PersistentHashMap extends PersistentEnumerator{ super(new ByteArrayOutputStream()); } + public int getBufferSize() { + return ((ByteArrayOutputStream)out).size(); + } + public void writeTo(OutputStream stream) throws IOException { ((ByteArrayOutputStream)out).writeTo(stream); } @@ -63,6 +67,8 @@ public class PersistentHashMap extends PersistentEnumerator{ } } + private int myBytesInMemoryCount = 0; + private final LimitedPool myStreamPool = new LimitedPool(10, new LimitedPool.ObjectFactory() { public AppendStream create() { return new AppendStream(); @@ -92,6 +98,8 @@ public class PersistentHashMap extends PersistentEnumerator{ updateValueId(id, headerRecord); myStreamPool.recycle(value); + + myBytesInMemoryCount -= bytes.length; } catch (IOException e) { throw new RuntimeException(e); @@ -187,7 +195,18 @@ public class PersistentHashMap extends PersistentEnumerator{ public synchronized void appendData(Key key, ValueDataAppender appender) throws IOException { synchronized (ourLock) { markDirty(true); - appender.append(myAppendCache.get(key)); + + final AppendStream stream = myAppendCache.get(key); + final int sizeBefore = stream.getBufferSize(); + appender.append(stream); + myBytesInMemoryCount += (stream.getBufferSize() - sizeBefore); + + if (myBytesInMemoryCount > APPEND_CACHE_DATA_THRESHOLD) { + LOG.warn( + "PersistentHashMap: OVER " + APPEND_CACHE_DATA_THRESHOLD + " BYTES IN APPEND STREAM CACHE, FORCING CACHE FLUSH (not optimal serialization format?) File: " + getDataFile(myFile).getPath() + ); + myAppendCache.clear(); + } } }