From 5e8ee4c6863a1fe70c6db8d888d9213445cd41ee Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Tue, 12 Apr 2016 23:24:12 +0200 Subject: [PATCH] drop inmemory caches for unsaved documents on low memory (IDEA-153382) --- .../com/intellij/psi/stubs/StubIndexImpl.java | 2 +- .../util/indexing/FileBasedIndexImpl.java | 2 +- .../intellij/util/indexing/MapReduceIndex.java | 8 ++++++++ .../util/indexing/MemoryIndexStorage.java | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java b/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java index a295767004a8..84ce4d8c5183 100644 --- a/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java @@ -148,7 +148,7 @@ public class StubIndexImpl extends StubIndex implements ApplicationComponent, Pe extension instanceof StringStubIndexExtension && ((StringStubIndexExtension)extension).traceKeyHashToVirtualFileMapping() ); - final MemoryIndexStorage memStorage = new MemoryIndexStorage(storage); + final MemoryIndexStorage memStorage = new MemoryIndexStorage(storage, indexKey); MyIndex index = new MyIndex<>(new IndexExtension() { @NotNull @Override diff --git a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java index cb08da424c04..e7682bc88d34 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java @@ -388,7 +388,7 @@ public class FileBasedIndexImpl extends FileBasedIndex { } state.registerIndex(name, - createIndex(extension, new MemoryIndexStorage(storage)), + createIndex(extension, new MemoryIndexStorage(storage, name)), new FileBasedIndex.InputFilter() { @Override diff --git a/platform/lang-impl/src/com/intellij/util/indexing/MapReduceIndex.java b/platform/lang-impl/src/com/intellij/util/indexing/MapReduceIndex.java index d3a7ca988288..436802bccbc6 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/MapReduceIndex.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/MapReduceIndex.java @@ -76,6 +76,14 @@ public class MapReduceIndex implements UpdatableIndex)myStorage).clearCaches(); + } finally { + getWriteLock().unlock(); + } + } flush(); } catch (StorageException e) { LOG.info(e); diff --git a/platform/lang-impl/src/com/intellij/util/indexing/MemoryIndexStorage.java b/platform/lang-impl/src/com/intellij/util/indexing/MemoryIndexStorage.java index 6055feb67a0a..8b8503bf1765 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/MemoryIndexStorage.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/MemoryIndexStorage.java @@ -36,6 +36,7 @@ public class MemoryIndexStorage implements IndexStorage @NotNull private final IndexStorage myBackendStorage; private final List myListeners = ContainerUtil.createLockFreeCopyOnWriteList(); + private final ID myIndexId; private boolean myBufferingEnabled; public interface BufferingStateListener { @@ -45,7 +46,12 @@ public class MemoryIndexStorage implements IndexStorage } public MemoryIndexStorage(@NotNull IndexStorage backend) { + this(backend, null); + } + + public MemoryIndexStorage(@NotNull IndexStorage backend, ID indexId) { myBackendStorage = backend; + myIndexId = indexId; } @NotNull @@ -85,6 +91,16 @@ public class MemoryIndexStorage implements IndexStorage } } + void clearCaches() { + if (myMap.size() == 0) return; + String message = "Dropping caches for " + (myIndexId != null ? myIndexId:this) + ", number of items:" + myMap.size(); + FileBasedIndexImpl.LOG.info(message); + + for(ChangeTrackingValueContainer v:myMap.values()) { + v.dropMergedData(); + } + } + @Override public void close() throws StorageException { myBackendStorage.close();