I Wrote a Huge-Ass Memory Leak
2008.01.15 21:54During daylight hours, I work on a CMS which gathers, accepts, and publishes content to a few different websites.
We have a lot of jobs that need to run — downloading feeds from external providers into our normalized Atom feed store, ingesting feeds into our content database so they can be used to compose content, and publishing articles, photo galleries, and editorial content to our websites.
In OSGi, declarative service instances are singletons by default. Another option is to specify your service as a service factory, causing each client bundle to get its own instance of the service.
This allows me to do some defensive cleanup — if a client bundle goes away, all of the jobs submitted by that bundle are canceled and removed from the system.
Works well — except that I forgot to clear the jobs from my service factory’s cache after they exited. And, over the course of a few days, the cache grew to contain tens of thousands of jobs, and consumed memory until the application froze and the garbage collector churned.
The 2 most important rules of object caching:
- Don’t cache.
- If you must cache, be sure to define a cache exit policy.
- Don’t forget to actually implement the cache exit policy.
category: code
tags: confession, java, memory, osgi, performance