High CPU consumption with Java-21 + Nashorn 15.4

Packiaraj S s.packiaraj at gmail.com
Fri Oct 25 15:20:36 UTC 2024


Hello,

We use Nashorn with Tomcat as a long running service. We recently migrated
to Java 21. ( from java 11 and the performance is good with Java-11).
Since Nashorn is moved out of JDK we have pulled in '*nashorn-core-15.4.jar*'
and its dependency (*asm**) and loaded it as a regular jar.
Functionality everything looks ok.

During the performance test we observed very high cpu usage when nashorn
engine's 'eval' is called.  The CPU consumption is so high that instances
are throttled and performance becomes 10x slower compared to java-11.

Upon investigation using profiler (jvisualVM) looks like most of the CPU is
spent in compile method
<https://github.com/openjdk/nashorn/blob/main/src/org.openjdk.nashorn/share/classes/org/openjdk/nashorn/internal/runtime/Context.java#L1454>,
more specifically during ContextCodeInstaller.initialize
<https://github.com/openjdk/nashorn/blob/main/src/org.openjdk.nashorn/share/classes/org/openjdk/nashorn/internal/runtime/Context.java#L217>
and NamedContextCodeInstaller.install
<https://github.com/openjdk/nashorn/blob/main/src/org.openjdk.nashorn/share/classes/org/openjdk/nashorn/internal/runtime/Context.java#L290>


Looks like the compile method is optimized with cache, unfortunately the
cache is in context scope, meaning it's not shared between ScriptEngines
and Nashorn is not thread-safe (as per the docs) to use a single instance
of ScriptEngine across all threads. Also the cache uses 'soft-reference',
would it cause double whammy when there is a memory pressure.

so, how to improve the performance of the Nashorn engine, specifically the
`compile` part.
Is there any other option we can try? BTW,  persistent-code-cache did not
help as OptimisticTypesPersistence.getVersionDirName is performing poorly

Test code that we used to study this high CPU issue is attached.

Thanks a lot for hemp & any insight
Sakkanan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/nashorn-dev/attachments/20241025/f2bbaa0f/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: NashornEngineTester.java
Type: application/octet-stream
Size: 3098 bytes
Desc: not available
URL: <https://mail.openjdk.org/pipermail/nashorn-dev/attachments/20241025/f2bbaa0f/NashornEngineTester.java>


More information about the nashorn-dev mailing list