Memory Leaks? We have a Garbage Collector!
True. Java comes with a Garbage Collector and makes reclaiming of heap space easier. On the other hand, Java offers a number of features, which, if used carelessly, can be the root for your memory leak nightmares.
Simple and Tempting
Since JDK 1.1 we have a quick, simple and tempting tool within the Core Language to create our own memory leaks. It doesn't matter whether we build server or standalone applications. This tool is called Inner Class. Together with its twin Anonymous Class both are very efficient in creating memory leaks.
public class Outer {
class Inner {
}
}How is this?
Every non-static Inner Class has an implicit reference to its surrounding class. Anonymous Classes are similar. To successfully create a memory leak simply pass an Inner Class object to a method which keeps references to the provided objects and you're done.
Why?
Suppose you implement something like a cache. Next follow the execution path to a local object which stores some of its inner class objects into the cache. After the local object was out of scope, it won't be garbage collected anymore! The inner class object in the cache holds a reference to the surrounding object and that one is still referenceable and therefore not a candidate for garbage collection anymore. The same is true for anonymous classes!
How to prevent this style of Memory Leaks?
- If you're about to use Inner Classes or Anonymous Classes think carefully. Don't use Anonymous Classes until you're very sure and can prove that they are not causing a Memory Leak.
- Use a static Inner Class to get rid of the implicit outer class reference.
Comments (5)
There's a simple way to avoid this. Simply declare the inner class static, and it won't get that implicit reference to the outer class.
The flip side of this is that you no longer get access to the surrounding class' instance variables and / or methods.
Posted by Asgeir S. Nilsen | September 19, 2007 4:27 AM
Posted on September 19, 2007 04:27
Have you read about island of isolation? Garbage Collector could handle this situation already.
Kind Regards
Posted by Marcos Silva Pereira | September 19, 2007 10:32 PM
Posted on September 19, 2007 22:32
Marcos, you're right, a proper Island of Isolation is not problematic. The problem are overlooked references and that's why
Inner/Anonymous Classes are problematic because of the implicit reference...
Posted by Olaf Heimburger | September 20, 2007 2:32 AM
Posted on September 20, 2007 02:32
the cache should store a weak reference of the inner class object
Posted by pavan | January 9, 2008 12:34 PM
Posted on January 9, 2008 12:34
True, the implicit reference does keep some memory alive (how else would SuperClass.this work?), but this is not a memory leak. The outer object is kept in memory because the inner object can still use it; indeed, if you don't need it, declare the inner class static. In the end, the outer class is still reachable.
It would be a memory leak if the outer class would be unreachable, and still remain in memory (for instanced, by some circular reference, which is what the Island of Isolation that Marcos talked about is about), such as in non-garbage-collected languages, when deleting a pointer without releasing its heap space.
Still, I agree with you that "if you're not gonna use it, don't make it", so tell the compiler you don't need that reference.
Posted by Angelo vd Sijpt | January 9, 2008 3:35 PM
Posted on January 9, 2008 15:35