ScriptEngine clear and dispose

bvdb

My application uses a ScriptEngine to offer plugin-ability to my end-users.

ScriptEngineManager engineManager = new ScriptEngineManager();
ScriptEngine engine = engineManager.getEngineByName("nashorn");

Whenever a user makes changes to his script, then the application replaces the engine instane by a new instance.

String newScript = ...;
engine = engineManager.getEngineByName("nashorn");
engine.eval(newScript);

Two closely related questions:

  • Should I try to reuse engines and perform some kind of clear() on them ?

  • If I just replace my engine with a new instance, should I dispose the previous instance in some way, to avoid memory leaks ? (e.g. I can imagine that the user could manage to create a script that starts a thread.)

The problem is, I cannot find any method that looks like a clear() or a dispose(). Does that mean that my current approach is correct ?

Attila Szegedi

You can use a single engine instance but use separate Bindings objects. Bindings acts as a top-level program environment, so if you want to evaluate a script into what is basically a "new global scope" then you could do that. Look into javax.script API docs on how to do this. You can either use ScriptEngine.eval that takes a Bindings as second argument or the one that takes ScriptContext as second argument.

Even if there's no script code surviving from the previous evaluation, you'll save some initialization time as the script engine will already have predefined various JavaScript data-holder classes and property maps ("hidden classes").

Also: yes, everything is garbage collected. There's no need for an explicit "disposal" API.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to clear or dispose current page in Crystal Report?

From Dev

How to clear or dispose current page in Crystal Report?

From Dev

How keep the memory clear in the C# applications (DataTable dispose)?

From Dev

Reuse Nashorn ScriptEngine in Servlet

From Dev

How to use ScriptEngine in ScalaTest

From Dev

Bindings HashMap with java ScriptEngine

From Dev

What is the "ScriptEngine" function in JavaScript?

From Dev

Java ScriptEngine supported languages

From Dev

Multi Threading Java ScriptEngine

From Dev

Bindings HashMap with java ScriptEngine

From Dev

How to use ScriptEngine in ScalaTest

From Dev

ScriptEngine with name "nashorn" is null

From Dev

Java 8 ScriptEngine across ClassLoaders

From Dev

Pass object instance to Roslyn ScriptEngine

From Dev

Java ScriptEngine and parent window functions

From Dev

Java Load a script by default in ScriptEngine

From Dev

"document" is not defined in <eval> java ScriptEngine

From Dev

How to use ScriptEngine in C#

From Dev

CacheManager and Dispose

From Dev

CacheManager and Dispose

From Dev

Dispose FileInfo()

From Dev

Dispose will dispose objects that are still referenced?

From Dev

PageDown through ScriptEngine incorrectly parsing Markdown

From Dev

ScriptEngine works in java String, but fails in String[]

From Dev

Nashorn: concurrent eval with singleton ScriptEngine ? thread safe?

From Dev

Why ScriptEngine has not Execute method anymore?

From Dev

Sbt 0.13 ScriptEngine is Null for getEngineByName(“JavaScript”)

From Dev

Checking if a function exists within java ScriptEngine

From Dev

Use a jar in JavaScript through Java ScriptEngine