From 31ceb1e7df60c821398e804acd7be443f03f8153 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Wed, 28 Mar 2012 20:55:35 +0400 Subject: [PATCH] KT-1636 Idea consumes the whole heap with Kotlin plugin #KT-1636 fixed --- .../jet/compiler/CompileEnvironment.java | 63 ++++++++++++------- .../jet/plugin/compiler/JetCompiler.java | 10 +-- 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/jet/compiler/CompileEnvironment.java b/compiler/cli/src/org/jetbrains/jet/compiler/CompileEnvironment.java index 99440faf8b5..500400e158c 100644 --- a/compiler/cli/src/org/jetbrains/jet/compiler/CompileEnvironment.java +++ b/compiler/cli/src/org/jetbrains/jet/compiler/CompileEnvironment.java @@ -69,6 +69,13 @@ public class CompileEnvironment { this(MessageRenderer.PLAIN, false); } + /** + * NOTE: It's very important to call dispose for every object of this class or there will be memory leaks. + * @see Disposer + * + * @param messageRenderer + * @param verbose + */ public CompileEnvironment(MessageRenderer messageRenderer, boolean verbose) { this.verbose = verbose; myRootDisposable = new Disposable() { @@ -170,38 +177,46 @@ public class CompileEnvironment { public boolean compileModuleScript(String moduleScriptFile, @Nullable String jarPath, @Nullable String outputDir, boolean jarRuntime) { CompileEnvironment moduleCompilationEnvironment = copyEnvironment(false); - moduleCompilationEnvironment.myStdlib = myStdlib; + try { + moduleCompilationEnvironment.myStdlib = myStdlib; - List modules = moduleCompilationEnvironment.loadModuleScript(moduleScriptFile); + List modules = moduleCompilationEnvironment.loadModuleScript(moduleScriptFile); - if (modules == null) { - throw new CompileEnvironmentException("Module script " + moduleScriptFile + " compilation failed"); - } - - if (modules.isEmpty()) { - throw new CompileEnvironmentException("No modules where defined by " + moduleScriptFile); - } - - final String directory = new File(moduleScriptFile).getParent(); - for (Module moduleBuilder : modules) { - CompileEnvironment compileEnvironment = copyEnvironment(verbose); - ClassFileFactory moduleFactory = compileEnvironment.compileModule(moduleBuilder, directory); - if (moduleFactory == null) { - return false; + if (modules == null) { + throw new CompileEnvironmentException("Module script " + moduleScriptFile + " compilation failed"); } - if (outputDir != null) { - writeToOutputDirectory(moduleFactory, outputDir); + + if (modules.isEmpty()) { + throw new CompileEnvironmentException("No modules where defined by " + moduleScriptFile); } - else { - String path = jarPath != null ? jarPath : new File(directory, moduleBuilder.getModuleName() + ".jar").getPath(); + + final String directory = new File(moduleScriptFile).getParent(); + for (Module moduleBuilder : modules) { + CompileEnvironment compileEnvironment = copyEnvironment(verbose); try { - writeToJar(moduleFactory, new FileOutputStream(path), null, jarRuntime); - } catch (FileNotFoundException e) { - throw new CompileEnvironmentException("Invalid jar path " + path, e); + ClassFileFactory moduleFactory = compileEnvironment.compileModule(moduleBuilder, directory); + if (moduleFactory == null) { + return false; + } + if (outputDir != null) { + writeToOutputDirectory(moduleFactory, outputDir); + } + else { + String path = jarPath != null ? jarPath : new File(directory, moduleBuilder.getModuleName() + ".jar").getPath(); + try { + writeToJar(moduleFactory, new FileOutputStream(path), null, jarRuntime); + } catch (FileNotFoundException e) { + throw new CompileEnvironmentException("Invalid jar path " + path, e); + } + } + } finally { + compileEnvironment.dispose(); } } + return true; + } finally { + moduleCompilationEnvironment.dispose(); } - return true; } private CompileEnvironment copyEnvironment(boolean verbose) { diff --git a/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java b/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java index 725053e9bb0..ac9602b18f3 100644 --- a/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java +++ b/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java @@ -262,7 +262,7 @@ public class JetCompiler implements TranslatingCompiler { } private static int execInProcess(File kotlinHome, VirtualFile outputDir, File scriptFile, PrintStream out, CompileContext context) { - URLClassLoader loader = getOrCreateClassloader(kotlinHome, context); + URLClassLoader loader = getOrCreateClassLoader(kotlinHome, context); try { String compilerClassName = "org.jetbrains.jet.cli.KotlinCompiler"; Class kompiler = Class.forName(compilerClassName, true, loader); @@ -288,13 +288,13 @@ public class JetCompiler implements TranslatingCompiler { } } - private static SoftReference ourClassloaderRef = new SoftReference(null); + private static SoftReference ourClassLoaderRef = new SoftReference(null); - private static URLClassLoader getOrCreateClassloader(File kotlinHome, CompileContext context) { - URLClassLoader answer = ourClassloaderRef.get(); + private static URLClassLoader getOrCreateClassLoader(File kotlinHome, CompileContext context) { + URLClassLoader answer = ourClassLoaderRef.get(); if (answer == null) { answer = createClassloader(kotlinHome, context); - ourClassloaderRef = new SoftReference(answer); + ourClassLoaderRef = new SoftReference(answer); } return answer; }