Does Java garbage collection guarantee that a program will not run out of memory?

23/04/2008

No. Java Programs may use up memory resources faster than they are garbage collected. A Java program can create objects that are not subject to garbage collection.

It is perfectly possible for a programmer to mistakingly create objects which never go out of scope, thus consuming more and more memory until all heap is exhausted.

It is the programmer’s responsibility to ensure that objects no longer in use are no longer referenced by the application. That way the garbage collector can do its job and reclaim memory used by these objects.

Try out the following code and check wether the GC guarentee that this application will not run out of memory

public class Main {
public static void main(String[] main) {
List l = new LinkedList();
// Enter infinite loop which will add a String to the list: l on each iteration.
do {
l.add(new String(“Hello, World”));
} while(true);
}
}

chandana on February 23rd, 2011 9:22 am

vbngfhjg

ghjg on May 5th, 2011 1:25 pm

nice example….

Achal on January 16th, 2012 12:11 pm

Garbage collection does not guarantee that a program will not run out of memory.

•Consider object you don’t want to use any more are like garbage.

•References to those objects will be like having that garbage in your house.

•Garbage collection is like your town’s garbage truck that collects garbage.

•If you won’t release those references, it is like not taking garbage out and soon your house will be over filled with garbage as garbage truck guys won’t take out garbage from your house.

Unreferenced objects will be garbage collected automatically by garbage collector. In java, most references to objects are released automatically once you come out of method.

Objects have reference to other objects, which in turn referr to other objects creating whole object graph. So as such object can be referenced by more than one object.

•If object is having zero references, it is eligible for garbage collection.

•Objects are allocated on heap.

•Garbage collector runs from time to time to delete unreferenced objects from heap.

•If you keep creating more objects on heap without releasing you will eventually get OutOfMemoryError

Java learner on February 8th, 2012 5:33 pm

The object is eligible for garbage collection only if there is no strong reference to the object.

An exception to this is island of references(where all objects point to each other).

One should never write his/her significant code in the object’s finalise method because you can never be very sure that it will run.

Finalise method runs only once in the life-time of an object. So even if you re-initialise an object in its finalize code, it can become eligible for garbage collection(if there is no strong reference to the object) and its finalise method won’t be called again

Bhaskar on February 8th, 2012 5:38 pm

Are Static Objects eligible for Garbage Collection???

Anonymous on February 8th, 2012 5:39 pm

Static references are not eligible for garbage collection.

Arnab Nandan on February 21st, 2012 12:46 pm

Not related to this article, but it would be good if you can open the facebook, twitter etc links on a new tab using target=”_blank” in your tag.

Souvik Banerjee on March 22nd, 2012 5:40 pm

Thanks Souvik. Sure I will do.

Joe on March 24th, 2012 10:47 pm

No the garbage collector does not gaurentee that the program will run out of memeory.

Anonymous on May 16th, 2012 3:02 pm


Email:

about
I am Joe, author of this blog. I run this with loads of passion. If you are into java, you may find lot of interesting things around ...more about me. Google+
java badge
Home