graaljs: GraalJS memory leak in Engine
Hello. I’m creating service like FaaS for JS scripts with limited API and found memory leak issues on prototyping different approaches while using Graal Polyglot.
Approaches that I’ve trying to implement:
- Pool of Context with single Engine. Source evaluated once in Context. Any JS code wrapped to unique namespace, that can be accessible as member of Context and executed. Any other invocations just execute existing member and nothing more
- Single Engine, Sources are cached. On any new invocation we just getting existing Source and evaluate them in new Context that created for every invocation. After that getting function member from Context and execute (as in previous approach)
- New Engine and New Context for every invocation, Sources are built once.
Only third approach have no memory leaks, because GC will grab them out from heap. In first and second approaches are memory leaks. Looks like Engine have issues on executing some code and generates much data for each invocation, that will be never removed from Engine.
I have a prototype that reproduces the first approach: https://github.com/mdsina/graaljs-executor-web-service
Also I wrote a load tests that simulate real-world invocations of JS code: https://github.com/mdsina/graaljs-executor-web-service-load-tests/tree/master Service was executed with VM options:
-Xmx2g -XX:+UseG1GC

Looks like links to ContextJS never be removed.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 39 (9 by maintainers)
Commits related to this issue
- [jsscripting] Fix memory-leak caused by com.oracle.truffle.host.HostObject Fixes this memory leak by making the HostAccess for the GraalJSScriptEngine available in a static final variable instead of ... — committed to florian-h05/openhab-addons by florian-h05 2 years ago
- [jsscripting] Fix memory leak that crashes openHAB (#13824) * [jsscripting] Fix memory-leak caused by com.oracle.truffle.host.HostObject Fixes this memory leak by making the HostAccess for the Gra... — committed to openhab/openhab-addons by florian-h05 2 years ago
- [jsscripting] Fix memory leak that crashes openHAB (#13824) * [jsscripting] Fix memory-leak caused by com.oracle.truffle.host.HostObject Fixes this memory leak by making the HostAccess for the Gra... — committed to morph166955/openhab-addons by florian-h05 2 years ago
- [jsscripting] Fix memory leak that crashes openHAB (#13824) * [jsscripting] Fix memory-leak caused by com.oracle.truffle.host.HostObject Fixes this memory leak by making the HostAccess for the Gra... — committed to andrasU/openhab-addons by florian-h05 2 years ago
- [jsscripting] Share org.graalvm.polyglot.Engine across all OpenhabGraalJSScriptEngine instances See https://github.com/oracle/graaljs/issues/121#issuecomment-880056648, it is not required to have one... — committed to florian-h05/openhab-addons by florian-h05 2 years ago
- [jsscripting] Improve performance & reduce memory usage (#14113) * [jsscripting] Share org.graalvm.polyglot.Engine across all OpenhabGraalJSScriptEngine instances See https://github.com/oracle/gra... — committed to openhab/openhab-addons by florian-h05 2 years ago
- [jsscripting] Fix memory leak that crashes openHAB (#13824) * [jsscripting] Fix memory-leak caused by com.oracle.truffle.host.HostObject Fixes this memory leak by making the HostAccess for the Gra... — committed to borazslo/openhab-mideaac-addon by florian-h05 2 years ago
- [jsscripting] Improve performance & reduce memory usage (#14113) * [jsscripting] Share org.graalvm.polyglot.Engine across all OpenhabGraalJSScriptEngine instances See https://github.com/oracle/gra... — committed to borazslo/openhab-mideaac-addon by florian-h05 2 years ago
- [jsscripting] Fix memory leak that crashes openHAB (#13824) * [jsscripting] Fix memory-leak caused by com.oracle.truffle.host.HostObject Fixes this memory leak by making the HostAccess for the Gra... — committed to psmedley/openhab-addons by florian-h05 2 years ago
- [jsscripting] Fix memory leak that crashes openHAB (#13824) * [jsscripting] Fix memory-leak caused by com.oracle.truffle.host.HostObject Fixes this memory leak by making the HostAccess for the Gra... — committed to nemerdaud/openhab-addons by florian-h05 2 years ago
- [jsscripting] Improve performance & reduce memory usage (#14113) * [jsscripting] Share org.graalvm.polyglot.Engine across all OpenhabGraalJSScriptEngine instances See https://github.com/oracle/gra... — committed to nemerdaud/openhab-addons by florian-h05 2 years ago
- [jsscripting] Improve performance & reduce memory usage (#14113) * [jsscripting] Share org.graalvm.polyglot.Engine across all OpenhabGraalJSScriptEngine instances See https://github.com/oracle/gra... — committed to renescherer/openhab-addons by florian-h05 2 years ago
- [jsscripting] Fix memory leak that crashes openHAB (#13824) * [jsscripting] Fix memory-leak caused by com.oracle.truffle.host.HostObject Fixes this memory leak by making the HostAccess for the Gra... — committed to andrasU/openhab-addons by florian-h05 2 years ago
Hi,
I observed similar symptoms to the ones described by @horschi. Also his solution worked in my case (btw thanks Christian!).
I prepared a reproducer project for this memory leak https://github.com/xardasos/GraalVMMemoryLeak. @wirthi could you please take a look at it? On my PC after around 23000 iterations I get OOM error (VM options: ‘-Xmx512m’).
Regards, Tomasz