Memory leak in JPython beta 2

Barry A. Warsaw bwarsaw at cnri.reston.va.us
Tue Oct 5 16:11:52 EDT 1999


>>>>> "rhays" == <rhays at interaccess.com> writes:

    rhays> I run the following program for about 200+ iterations and
    rhays> see a pretty reasonable memory leak; after 800 passes, the
    rhays> leak is large.  Has anyone else seen this with the beta2
    rhays> version?  Does the new beta 3 version fix the (at least
    rhays> apparent) memory leak?  I'm running JDK 1.1.7 on HP-UX
    rhays> 10.20.

    rhays> This is a serious problem for us as we want to use JPython
    rhays> to translate XML formats for a large number of messages.

Most JVMs don't free memory associated with classes, even when
the classes are no longer reachable.  For this reason, the exec()
method in JPython leaks.  JimH has given me a patch which may fix the
problem, but I haven't had time to look at it in detail yet.

In the meantime, a simple rewrite of your Java class should alleviate
your memory leaks.  The key here is to create only one interpreter,
execfile your script, dig the function you're interested in out of it,
and then just call your function repeatedly.  Here's a rewrite which
illustrates the approach.

-------------------- snip snip --------------------interp.py
def execute(i):
    print 'execute(%d)' % i
    return
-------------------- snip snip --------------------jinterp.java
import org.python.core.*;
import org.python.util.PythonInterpreter;

class jinterp
{
    private static final int SLEEP_INTERVAL = 100;

    public static void main(String[] args) {
        String script = "interp.py";
        PythonInterpreter interp = new PythonInterpreter();
        interp.execfile(script);
        PyObject execfunc = interp.get("execute");
        Runtime rt = Runtime.getRuntime();
        for (int i=0; i < 100000; i++) {
            execfunc.__call__(new PyInteger(i));
            System.gc();
            long freemem = rt.freeMemory();
            long totalmem = rt.totalMemory();
            System.err.println("free: " + freemem + ", total: " + totalmem);
            try {
                Thread.sleep(SLEEP_INTERVAL);
            }
            catch (Exception e) {
            }
        }
    }
}
-------------------- snip snip --------------------

-Barry




More information about the Python-list mailing list