Dear Tobia,

In order to close and clean up a Servlet process under Jython (PyServlet),  I use to move a None value into the larger objects, like arrays and dictionaries and call the garbage collection routine in a manual call, like for example:  
            from java.lang import System
            mydictionary = None

I also use to call the normal servlet close routines (also check eventual exceptions), like:
           stmt.close()  # database statement
             conn.close()  # database driver connection
           out.close()   # servlet output stream
In case of any output redirection, restore standard output
              sys.stdout = sys.__stdout__

This might help to reduce or completely avoid the leaks.


Claude Falbriard
Certified IT Specialist L2 - Middleware
AMS Hortolāndia / SP - Brazil
phone:    +55 19 9837 0789
cell:         +55 13 8117 3316

From:        Tobia Conforto <>
Date:        05/04/2012 14:19
Subject:        Re: [Jython-users] How can I shut down Jython cleanly?


Does anybody know how to restart a Jython interpreter without leaks?

> Hello
> I'm developing a bridge, based on Jython, to allow the development of
> J2EE Portlets with the Django framework. I need some advice on how to
> shut down or reload the Jython interpreter correctly, because right now
> I'm having some serious memory and thread leaks.
> The project includes a Java class and a few Python ones. The Java code
> implements the Portlet interface and is responsible for creating the
> Jython interpreter and forwarding all requests to the Python code. In a
> separate thread it monitors the Python sources for changes, reloading
> the Jython runtime whenever a change is detected. This class makes
> extensive use of concurrency tools to support reloading the runtime in
> the midst of many portlet requests, without any service disruption. The
> Python code also implements the Portlet interface and is responsible for
> translating Portlet requests into Django ones and the responses back,
> working around the differences between the Portlet paradigm and the HTTP
> one.
> But it all hinges on the Jython reloading code, which unfortunately is
> at the moment very leaky. This is what I do to create an interpreter:
> Py.setSystemState(new PySystemState());
> PySystemState.initialize(System.getProperties(), configProperties);
> jython = new PythonInterpreter();
> // cue library imports here
> And this is how I destroy it (mostly copied over from org.python.util.jython):
> thread.interruptAllThreads();
> Py.getSystemState()._systemRestart = true;
> try {
>    imp.load("socket").__findattr__("_closeActiveSockets").__call__();
> } catch (PyException pye) {}
> jython.cleanup();
> jython = null;
> With this code, my test deployment on Tomcat cannot withstand more than
> a few Jython restarts, after which I get an OutOfMemoryError, usually
> PermGen space. If I try to manually undeploy the portlet, Tomcat kindly
> lists the threads and other resources that are being leaked by the
> Jython interpreter. (see attached file)
> My questions to the knowledgeable is thus:
> What is the correct way to shut down (or restart, or reset) a Jython
> interpreter without leaking anything?
> I'm using 2.5.2, but I can test dev code if needed.
> -Tobia

Better than sec? Nothing is better than sec when it comes to
monitoring Big Data applications. Try Boundary one-second
resolution app monitoring today. Free.
Jython-users mailing list