The JSR223 script engine is stateful and not thread safe. (We should probably make it synchronized at all important points, so it could be used from multiple threads, but it will not make it lighter, because of statefulness.)
The state there is the state of the compilation and evaluation, which includes loaded dependencies and analyzed code, and many other data required for the compilation and evaluation of the next script that you can pass to the engine. Because the engine is in fact a REPL, so every next compilation and evaluation performed in the context of all previously compiled scripts.
So, keeping one engine running will increase amount of memory occupied by the state with every compilation.
On the other hand, the initialization of the state is expensive, so you may get the tradeoff between the speed of compilation and the memory consumption. You can probably balance between the extremes by destroying the old and creating the new engine only sometimes.
With the new scripting API you have more control of the state, although the general problems remain the same. But you may also use compiled scripts caching interface to avoid compilation when possible. This one is not available in the JSR-223 interface.
The new API is available since some releases already, but in the experimental state. And will remain experimental for some time. It is quite stable though, we are not going to break it often, if at all, but we’d like to have this freedom for a moment, to be able to fix possible problems easier.
And then documentation is mostly missing now. But there is a new repository with examples, so maybe it will be enough to start - GitHub - Kotlin/kotlin-script-examples: Examples of Kotlin Scripts and usages of the Kotlin Scripting API. Some concepts are described in this KEEP (KEEP/scripting-support.md at master · Kotlin/KEEP · GitHub) as well.
BTW, the KotlinJsr223JvmLocalScriptEngine
is only an example, and somewhat obsolete. The official basic JSR-223 implementation is distributed in the kotlin-scripting-jsr223
artifact.