ops4j / org.ops4j.pax.web

OSGi R7 Http Service, Whiteboard and Web Applications (OSGi CMPN Release chapters 102, 140 and 128) implementation using Jetty 9, Tomcat 9 or Undertow 2.
https://ops4j1.jira.com/wiki/display/paxweb/Pax+Web
Other
146 stars 184 forks source link

When a bundle registered with Pax Web is uninstalled, references to that bundle's objects are not properly cleaned up causing a memory leak. [PAXWEB-191] #521

Closed ops4j-issues closed 14 years ago

ops4j-issues commented 14 years ago

John Trimble created PAXWEB-191

Pax Web continues to hold object references to uninstalled bundles that had registered with the Pax Web WebContainer service or were extended by the Pax Web War Extender. This causes a memory leak leading to permanent generation and heap space issues if bundles, which interact with Pax Web in the aforementioned ways, are routinely installed and uninstalled.

With regard to the WebContainer service, instances of org.ops4j.pax.web.service.internal.HttpServiceStarted are never garbage collected once instantiated and potentially continue to hold references to various objects from uninstalled bundles, such as ServletContextListener instances, if they were not explicitly unregistered. The cause of this is that newly instantiated instances of HttpServiceStarted add a ServerListener instance, implemented as an anonymous inner class of HttpServiceStarted, to the ServerController, but never remove it. This listener holds an implicit reference to the HttpServiceStarted instance preventing its garbage collection, and the garbage collection of anything it references.

The Pax Web War Extender's WebXmlObserver holds references to instances of org.ops4j.pax.web.extender.war.internal.model.WebApp even after the associated WAR has been uninstalled. This leads to the same issue regarding permanent generation and heap space.

I've attached a patch that should plug both of these leaks. First, it adds the ability to remove ServerListener instances from a ServerController, and modifies the HttpServiceStarted's stop() method to remove the appropriate ServerListener instance from the ServerController. Secondly, it modifies the WebXmlObserver's removingEntries() method to remove the appropriate WebApp reference.

In addition to the patch, I've included a small example webapp to help illustrate the problem. Simply install and uninstall the WAR serveral times, allowing the Pax Web War Extender to extend it, and an OutOfMemory exception will soon follow. If you examine the heap space, using a tool like YourKit, you will find outstanding references to MemoryHogServletContextListener instances, as well as extraneous WebApp and HttpServiceStarted instances.


Affects: 0.7.2 Fixed in: 0.7.3 Attachments:

Votes: 0, Watches: 1

ops4j-issues commented 14 years ago

NiclasH commented

Good work.
Perhaps you know that OPS4J stands for "Open Participation Software for Java", and the whole idea is that this kind of improvement can come from anywhere, without being a 'core developer' of a project (a la Apache styled committers), and instead we invite everyone to apply the patch straight into the codebase, and those who have a larger vested interest will peer review it.

So, please feel free to commit the code directly... If it upsets something delicate, it can easily be reverted.

ops4j-issues commented 14 years ago

John Trimble commented

Oh wow, that is pretty cool. I actually had no idea what OPS4J stood for. I apologize, I should of read through the OPS4J docs for contributors before posting this issue. Thank you for taking the time to put me on track.

ops4j-issues commented 14 years ago

John Trimble commented

Applied patch (r16692)