changeset 43757:7193f6ef25db

8173893: JShell: reduce memory leaks Reviewed-by: jlahoda
author rfield
date Wed, 08 Feb 2017 09:12:45 -0800
parents 118221d3960d
children 868af3718a21
files langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/debug/InternalDebugControl.java langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java langtools/src/jdk.jshell/share/classes/jdk/jshell/JShell.java
diffstat 3 files changed, 48 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/debug/InternalDebugControl.java	Wed Feb 08 06:43:34 2017 -0800
+++ b/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/debug/InternalDebugControl.java	Wed Feb 08 09:12:45 2017 -0800
@@ -90,6 +90,17 @@
     }
 
     /**
+     * Release a JShell instance.
+     *
+     * @param state the JShell instance
+     */
+    public static void release(JShell state) {
+        if (debugMap != null) {
+            debugMap.remove(state);
+        }
+    }
+
+    /**
      * Tests if any of the specified debug flags are enabled.
      *
      * @param state the JShell instance
--- a/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Wed Feb 08 06:43:34 2017 -0800
+++ b/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Wed Feb 08 09:12:45 2017 -0800
@@ -812,6 +812,12 @@
         }
     }
 
+    /**
+     * The entry point into the JShell tool.
+     *
+     * @param args the command-line arguments
+     * @throws Exception catastrophic fatal exception
+     */
     public void start(String[] args) throws Exception {
         OptionParserCommandLine commandLineArgs = new OptionParserCommandLine();
         options = commandLineArgs.parse(args);
@@ -842,30 +848,33 @@
                 hardmsg("jshell.msg.welcome", version());
             }
             // Be sure history is always saved so that user code isn't lost
-            Runtime.getRuntime().addShutdownHook(new Thread() {
+            Thread shutdownHook = new Thread() {
                 @Override
                 public void run() {
                     replayableHistory.storeHistory(prefs);
                 }
-            });
+            };
+            Runtime.getRuntime().addShutdownHook(shutdownHook);
             // execute from user input
             try (IOContext in = new ConsoleIOContext(this, cmdin, console)) {
-                start(in);
+                while (regenerateOnDeath) {
+                    if (!live) {
+                        resetState();
+                    }
+                    run(in);
+                }
+            } finally {
+                replayableHistory.storeHistory(prefs);
+                closeState();
+                try {
+                    Runtime.getRuntime().removeShutdownHook(shutdownHook);
+                } catch (Exception ex) {
+                    // ignore, this probably caused by VM aready being shutdown
+                    // and this is the last act anyhow
+                }
             }
         }
-    }
-
-    private void start(IOContext in) {
-        try {
-            while (regenerateOnDeath) {
-                if (!live) {
-                    resetState();
-                }
-                run(in);
-            }
-        } finally {
-            closeState();
-        }
+        closeState();
     }
 
     private EditorSetting configEditor() {
@@ -1019,6 +1028,8 @@
         live = false;
         JShell oldState = state;
         if (oldState != null) {
+            state = null;
+            analysis = null;
             oldState.unsubscribe(shutdownSubscription); // No notification
             oldState.close();
         }
@@ -2006,7 +2017,6 @@
     private boolean cmdExit() {
         regenerateOnDeath = false;
         live = false;
-        replayableHistory.storeHistory(prefs);
         fluffmsg("jshell.msg.goodbye");
         return true;
     }
--- a/langtools/src/jdk.jshell/share/classes/jdk/jshell/JShell.java	Wed Feb 08 06:43:34 2017 -0800
+++ b/langtools/src/jdk.jshell/share/classes/jdk/jshell/JShell.java	Wed Feb 08 09:12:45 2017 -0800
@@ -543,17 +543,7 @@
      */
     @Override
     public void close() {
-        if (!closed) {
-            closeDown();
-            try {
-                executionControl().close();
-            } catch (Throwable ex) {
-                // don't care about exceptions on close
-            }
-            if (sourceCodeAnalysis != null) {
-                sourceCodeAnalysis.close();
-            }
-        }
+        closeDown();
     }
 
     /**
@@ -826,6 +816,15 @@
             } catch (Throwable thr) {
                 // Don't care about dying exceptions
             }
+            try {
+                executionControl().close();
+            } catch (Throwable ex) {
+                // don't care about exceptions on close
+            }
+            if (sourceCodeAnalysis != null) {
+                sourceCodeAnalysis.close();
+            }
+            InternalDebugControl.release(this);
         }
     }