title: Nashorn Javascript engine using cpu by loading classes after a minute?
body: <p>Using Java8u60, nashorn  </p>
  
  <p>The objective is to have approx 2000 function-objects interact with each their own API object every 500 msec. Each script being approx 5 to 20 lines of code.  </p>
  
  <p>What i see is that after the initial burst of CPU usage used for compiling the script it is running quite nicely. But after approximately a minute, there is another burst of CPU usage and a massive amount of classes are being loaded.  </p>
  
  <p>I would like to know why this is happening (because it was already compiled right?), and what ways there are to influence this phenomenon.</p>
  
  <p>Below, the effect as seen by using JvisualVM.<br>
  <em>Initially:</em><br>
  <a href="https://i.stack.imgur.com/El8JE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/El8JE.png" alt="initial_running_and_compile"></a>  </p>
  
  <p><em>After a minute approx 10000 classes are loaded. See the cpu usage and spike in right-bottom corner graph:</em><br>
  <a href="https://i.stack.imgur.com/vM21D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vM21D.png" alt="running_a_minute_and_classesloaded"></a>  </p>
  
  <p>Some details:<br>
  Each function-object has its own <code>SimpleScriptContext</code>.<br>
  I use <code>Swingworkers</code> to firstly compile and on next invocations execute the compiled function.<br>
  I have a <code>ScriptEngine</code> instance per thread (<code>Swingworkers</code> use a pool of 10 threads hardcoded in JDK) in the hope that compiling doesn't block other threads.<br>
  Each <code>SwingWorker</code> instance executes a single <code>CompiledScript</code> for all function-objects having the same script-text only once and then exits.(There are approx 30 unique scripts.)<br>
  These SwingWorkers are instantiated and executed every 500msec.<br>
  This is the code i use for executing a function-object:</p>
  
  <pre><code>/* 
   * test for script change, if so, recompile 
   */
  if (e.cCompiledScript == null) {
    /* compile once, but don't when an error is not cleared */
    if (runner.cErrorString == null) {
      e.initCompiledScript(runner.getScript());
    }
  } else {
    /* run the CompiledScript in the (shared)engine with the provided private context */
    e.cCompiledScript.eval(runner.getScriptContext());
  }
  </code></pre>