The class cache

Tal Liron tal.liron at
Wed Mar 12 09:58:36 UTC 2014

I did see that change come through, but I thought it was a JDK9 
feature... great to know it is for JDK8!

I'll note that I'm not using ScriptEngine, but instead working on the 
Scripturian implementation. How will ScriptEngine make sure to reuse the 

Would it be possible to also allow a shared ClassCache instance for all 
globals? How about a ScriptEnvironment._class_cache_size boolean option 
to enable this?

On 03/12/2014 05:50 PM, Hannes Wallnoefer wrote:
> Hi Tal,
> I'm right now pushing a change for JDK-8021350 that allows sharing 
> script classes between global scopes. Currently, script classes are 
> bound to the global object in various ways, so the change is not 
> trivial, and it's not possible to share compiled scripts between 
> global scopes with the nashorn.jar in current JDK8 builds. The script 
> sharing feature is planned for the 8u20 release.
> The class sharing will be per script engine, meaning that if you use 
> multiple scopes with one script engine classes will be reused, when 
> you use multiple script engines scripts will be recompiled for each 
> engine.
> I'd be interested to know whether this would work for you. If you'd 
> like to test the class sharing feature I can help you getting started.
> Hannes
> Am 2014-03-12 10:27, schrieb Tal Liron:
>> In Nashorn, the ClassCache is set per Global instance.
>> This is fine if your application has only one global instance. 
>> However, my application design involves creating many Global 
>> instances. (Actually, I create and destroy them on the fly per user 
>> request in an HTTP server scenario.) The problem is that all code has 
>> to constantly be recompiled, and the cache is essentially never used. 
>> Since recompilation is so very expensive in Nashorn, this results in 
>> awful performance.
>> How can I implement a shared ClassCache? I can't extend and modify 
>> Global behavior, because it's a final class.
>> I've tried to cache ScriptFunction instances myself, but I get 
>> exceptions when I try to run them with a different Global instance 
>> than the one that created them.

More information about the nashorn-dev mailing list