From: Tobia C. <tob...@gr...> - 2012-03-27 14:21:31
|
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 |