Issue1746

classification
Title: memory leak from encodings + codecs.java causes PermGen memory exhaustion
Type: crash Severity: major
Components: Core Versions: Jython 2.5
Milestone:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: fwierzbicki Nosy List: adam.spiers, amak, bpedman, fwierzbicki, zyasoft
Priority: urgent Keywords:

Created on 2011-05-09.12:16:50 by adam.spiers, last changed 2014-05-22.00:21:50 by zyasoft.

Files
File name Uploaded Description Edit Remove
codecs-leak-MAT-screenshot1.png adam.spiers, 2011-05-09.12:23:56 screenshot from Eclipse Memory Analyzer Tool showing leak
codecs-leak-MAT-screenshot2.png adam.spiers, 2011-05-09.12:24:21 another screenshot from Eclipse Memory Analyzer Tool showing leak
Messages
msg6524 (view) Author: Adam Spiers (adam.spiers) Date: 2011-05-09.12:16:49
There is a PermGen memory leak caused by the combination of
codecs.java and the CPython Lib's encodings/__init__.py.  The code in
question is:

    # Register the search_function in the Python codec registry
    codecs.register(search_function)

which adds the search_function PyFunction to
org.python.core.codecs.searchPath which is a static PyList.  Each of
these PyFunctions has a func_globals StringMap containing a key
'codecs', whose value is a PyModule whose __dict__ is a StringMap
containing a key 'sys' (because CPython Lib's codecs.py imports
'sys'), whose value is a PySystemState instance.  So for every Jython
thread which imports encodings, that thread's PySystemState and many
related objects will never be garbage collected.  After enough Jython
threads (only around 20 in our case), PermGen is exhausted resulting
in an OutOfMemoryError.

Our workaround has been to modify codecs.java to check whether
search_function is already in the PyList and if not, avoid adding it
again, but I'm not sure whether this is optimal.  Maybe a better
solution would be to ensure that it is removed from the PyList during
thread cleanup - but only if this could be done without using
finalizers, which according to Joshua Bloch and others should be
avoided if at all possible.
msg7548 (view) Author: Brandon (bpedman) Date: 2012-12-19.16:51:06
We have been testing out Jython 2.7 and this can also be fixed by making the codecs attached to the system state rather than being static. So we created a CodecState class that basically implements all the same methods as codecs and redirected the methods in codecs back to the current PySystemState's CodecState. This way the codecs are cleaned up along with the system state and the codecs do not have to be synchronized (to avoid race conditions when populating the search function)

The problem with doing as stated in the initial description is that the search function from the first interpreter is going to be the only function loaded and would probably be the cause of another leak (the initial PySystemState would hang around since the search_function PyFunction object hangs around in the static codecs class...though this is unconfirmed)
msg7668 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2013-02-12.20:14:34
Brandon: any chance of getting your solution as a patch? Even better if you have tests (but not a requirement)?
msg7669 (view) Author: Brandon (bpedman) Date: 2013-02-13.04:21:34
Submitted as a pull request here https://bitbucket.org/jython/jython/pull-request/5/issue1746-fix-issue-with-static-codecs

Sorry I don't have any tests...if you need more info on how to reproduce let me know...the basic test is just to keep creating and destroying jython interpreters and within each one import the encodings module and watch the permgen memory increase, though it might take a while unless you import lots of other stuff to fill up the PermGen space more quickly.
msg7678 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2013-02-13.23:08:15
Thanks Brandon!
msg7739 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2013-02-25.18:52:05
Setting to urgent to poke my future self.
msg8518 (view) Author: Jim Baker (zyasoft) Date: 2014-05-22.00:21:50
Fixed in 7126:7ff23feeb526
History
Date User Action Args
2014-05-22 00:21:50zyasoftsetstatus: open -> closed
resolution: fixed
messages: + msg8518
nosy: + zyasoft
2013-03-02 13:49:47amaksetnosy: + amak
2013-02-25 18:52:05fwierzbickisetpriority: normal -> urgent
messages: + msg7739
versions: + Jython 2.5, - 2.5.1
2013-02-13 23:08:15fwierzbickisetmessages: + msg7678
2013-02-13 04:21:35bpedmansetmessages: + msg7669
2013-02-12 20:14:35fwierzbickisetmessages: + msg7668
2013-02-12 20:08:57fwierzbickisetpriority: normal
assignee: fwierzbicki
2013-01-24 17:03:24fwierzbickisetnosy: + fwierzbicki
2012-12-19 16:51:08bpedmansetnosy: + bpedman
messages: + msg7548
2011-05-09 12:24:21adam.spierssetfiles: + codecs-leak-MAT-screenshot2.png
2011-05-09 12:23:56adam.spierssetfiles: + codecs-leak-MAT-screenshot1.png
2011-05-09 12:16:50adam.spierscreate