Skip to content

Fix Jython memory leak #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 15, 2015
Merged

Fix Jython memory leak #7

merged 7 commits into from
Dec 15, 2015

Conversation

hinerm
Copy link
Member

@hinerm hinerm commented Dec 14, 2015

Jython eval acts like an interpreter - any intermittently declared variables can be preserved. The PythonInterpreter is also frustratingly persistent and resists clean-up attempts.

This has led to a memory leak in our Jython script language (see http://fiji.sc/bugzilla/show_bug.cgi?id=1203).

To fix this, a PhantomReference is used to monitor when its corresponding JythonScriptEngine is garbage collected, at which point the PythonInterpreter is forcefully cleaned of local variables.

To avoid mutual hard references, the JythonScriptEngine and containing ScriptModule must never be passed to the PythonInterpreter. Instead, they can be cached in a Java-side map to weak references.

Uses PhantomReferences to clean up after the interpreter
If the JythonScriptEngine or containing ScriptModule is set within the
PythonInterpreter, we will get a circular reference chain preventing
memory from the PythonInterpreter being reclaimed.

To avoid completely discarding these objects, we can keep them in a
shallow map to WeakReferences.
@hinerm
Copy link
Member Author

hinerm commented Dec 14, 2015

@ctrueden - any objections here?

@ctrueden
Copy link
Member

This is complex. I want to understand why it is necessary, so let's discuss a little before merging.

hinerm and others added 5 commits December 15, 2015 08:54
Add extensive commenting to explain why PhantomReferences are needed to
solve the memory leak identified in this implementation of the Python
script language.

Fixes http://fiji.sc/bugzilla/show_bug.cgi?id=1203
Pass a LogService to the thread polling the PhantomReference queue so
that exceptions can be logged.
This isolates and encapsulates the cleanup logic, so the
JythonScriptLanguage itself remains easy to understand.
This avoids them breaking the 80 character line limit,
and makes them a little nicer to read (IMHO).
ctrueden added a commit that referenced this pull request Dec 15, 2015
@ctrueden ctrueden merged commit 44208a2 into master Dec 15, 2015
@ctrueden ctrueden deleted the remove-leaks branch December 15, 2015 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants