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


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