Unreachable Memory not freed, Nashorn Demo

Thorsten Goetzke tg at freigmbh.de
Tue Feb 28 12:45:44 UTC 2017


Hello,

Back in January i posted about Unreachable Objects not claimed by the 
gc, i am finally able to produce a micro, see below. When I run the 
class below using -Xmx4g and take a memory snaphsot (hprof or yourkit, 
doesnt matter), I will see 2 LeakImpl Objects. These Objects have no 
reported path to root, yet they won't be collected. If i lower the heap 
space to -Xmx2g the Application throws java.lang.OutOfMemoryError: Java 
heap space.
@Jenny Zhang should I create a new bugreport, or will you take care of this?

Best Regards,
Thorsten Goetzke

package de.frei.demo;

import jdk.nashorn.api.scripting.NashornScriptEngine;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;

import javax.script.CompiledScript;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
import java.util.function.Function;


public final class LeakDemo {

     private static  NashornScriptEngine ENGINE = getNashornScriptEngine();
     private static CompiledScript SCRIPT;

     public static void main(String[] args) throws Exception {
         simulateLoad();
         simulateLoad();
         System.gc();
         Thread.sleep(1000000);

     }

     private static void simulateLoad() throws ScriptException {
         final CompiledScript compiledScript = getCompiledScript(ENGINE);
         compiledScript.eval(new SimplestBindings(new LeakImpl()));
     }

     private static NashornScriptEngine getNashornScriptEngine() {
         final NashornScriptEngineFactory factory = new 
NashornScriptEngineFactory();
         final NashornScriptEngine scriptEngine = (NashornScriptEngine) 
factory.getScriptEngine();
         return scriptEngine;
     }



     private static CompiledScript getCompiledScript(final 
NashornScriptEngine scriptEngine) throws ScriptException {
         if (SCRIPT == null) {
             SCRIPT = scriptEngine.compile("    var pivot = 
getItem(\"pivot\");");
         }
         return SCRIPT;
     }

     public  interface Leak {

         LiveItem getItem(String id);
     }


     public static final class LeakImpl implements Leak {
         private final byte[] payload = new byte[1024 * 1024 * 1024];


         @Override
         public LiveItem getItem(final String id) {
             return new LiveItem() {
             };
         }


     }

     public interface LiveItem {
     }

     public static final class SimplestBindings extends SimpleBindings {
         public SimplestBindings(Leak leak) {

             put("getItem",(Function< String, LiveItem>) leak::getItem);
         }
     }
}



More information about the hotspot-gc-use mailing list