clear zip file caches on low memory

This commit is contained in:
Eugene Zhuravlev
2014-07-11 21:42:38 +02:00
parent c81ebf57fe
commit 3b3fee84d9
2 changed files with 57 additions and 0 deletions
@@ -56,6 +56,7 @@ import org.jetbrains.jps.incremental.storage.BuildTargetConfiguration;
import org.jetbrains.jps.incremental.storage.OneToManyPathsMapping;
import org.jetbrains.jps.incremental.storage.OutputToTargetRegistry;
import org.jetbrains.jps.indices.ModuleExcludeIndex;
import org.jetbrains.jps.javac.JavacMain;
import org.jetbrains.jps.model.java.JpsJavaExtensionService;
import org.jetbrains.jps.model.java.compiler.JpsJavaCompilerConfiguration;
import org.jetbrains.jps.model.module.JpsModule;
@@ -176,6 +177,7 @@ public class IncProjectBuilder {
final LowMemoryWatcher memWatcher = LowMemoryWatcher.register(new Runnable() {
@Override
public void run() {
JavacMain.clearCompilerZipFileCache();
myProjectDescriptor.dataManager.flush(false);
myProjectDescriptor.timestamps.getStorage().force();
}
@@ -452,4 +452,59 @@ public class JavacMain {
}
}
private static class ZipFileIndexCleanupDataHolder {
@Nullable
static final Method cacheInstanceGetter;
@Nullable
static final Method cacheClearMethod;
static {
Method getterMethod = null;
Method clearMethod = null;
try {
//trying JDK 6
clearMethod = Class.forName("com.sun.tools.javac.zip.ZipFileIndex").getDeclaredMethod("clearCache");
clearMethod.setAccessible(true);
}
catch (Throwable e) {
try {
final Class<?> cacheClass = Class.forName("com.sun.tools.javac.file.ZipFileIndexCache");
clearMethod = cacheClass.getDeclaredMethod("clearCache");
getterMethod = cacheClass.getDeclaredMethod("getSharedInstance");
clearMethod.setAccessible(true);
getterMethod.setAccessible(true);
}
catch (Throwable ignored2) {
clearMethod = null;
getterMethod = null;
}
}
cacheInstanceGetter = getterMethod;
cacheClearMethod = clearMethod;
}
}
private static volatile boolean zipCacheCleanupPossible = true;
public static void clearCompilerZipFileCache() {
if (zipCacheCleanupPossible) {
final Method clearMethod = ZipFileIndexCleanupDataHolder.cacheClearMethod;
if (clearMethod != null) {
final Method getter = ZipFileIndexCleanupDataHolder.cacheInstanceGetter;
try {
Object instance = getter != null? getter.invoke(null) : null;
clearMethod.invoke(instance);
}
catch (Throwable e) {
zipCacheCleanupPossible = false;
}
}
else {
zipCacheCleanupPossible = false;
}
}
}
}