views:

186

answers:

7

If any object variable is still pointing to some object which is of no use, then JVM will not garbage collect that object and object will remain in memory creating memory leak

In the above scenario there is possibility of memory leak.. Why is it not Garbage Collected? Can anybody elaborate on it?

+2  A: 

A typical example of this is an event listener. Whenever an event listener is registered, a reference to that class is held. If the object is disposed but the event listener not unregistered, the object never gets released from memory because of the reference to the event listener.

More info here: http://www.javaworld.com/javaworld/javatips/jw-javatip79.html

Chris
+1  A: 

Because if an object that is in use still has a reference to another object that you'd like to be collected, the in-use object can still use and take action on the object you want to be garbage collected. If an object is of no use then the reference to it should be removed so that the object can be garbage collected.

Garbage collection itself doesn't prevent you from memory leaks. There are scenarios where it's very possible to leak memory. You've just found one of those many scenarios.

Brian Hasden
+2  A: 

As long as someone has a reference to an object it can be used and the JVM cannot garbage collect it.

The JVM determines if clusters of objects are still in use by looking for a path of references from the root to the objects in question. This means that a cluster of objects that reference eachother but are not referenced from the rest of the system will get garbage collected.

So simply having an object pointing at you will not prevent you from being garbage collected.

rsp
+2  A: 

A typical scenario where this can happen is when you have a static Map, that is filled, but never cleared out. The Map cannot be garbage collected, since it is statically referred to, and the entries in the Map cannot be garbage collected, since they are referred to from the Map.

Paul Wagland
A: 

If you have a reference to the object, how is the JVM supposed to know that it's of "no use"? If you don't need it, drop the reference and the JVM can tell that you don't intend to use it any longer. The GC in Java is pretty good, but it can't read your mind or determine your codes intent.

Todd R
A: 

It's not garbage collected because the JVM cannot tell if the reference in that object variable will ever be followed in the future. Making such a determination would, in the general case, require the JVM to solve the halting problem.

Novelocrat
+2  A: 

Ideally, an automatic memory manager would release objects that will not be of any further use for the application.

It has been proven that it is not possible to determine, with 100% accuracy and in all situations, whether a given object will be used afterwards. Instead, garbage collectors use the "next best thing" which is that they release objects which are not reachable: an object for which cannot be accessed from the application, for lack of a path of references to that object, will never be used again by the application since there is no way for the application to even notice that the object still exists. It is an approximation but it is safe: the GC will not release an object that is still in use, but it may fail to release an object that will not be used any further, if that object appears to be reachable (i.e. the application may still reach out and grab the object, if it wishes so).

A "memory leak" is a situation where unused objects use an inordinate amount of RAM. What is "inordinate" depends on the application. A single unused object is rarely a problem. Usual important leaks are situations where unused-but-reachable objects accumulate by thousands.

Thomas Pornin