What is garbage collection in Java, and how can it be used ?

Answer Posted / prashant srivastava the great

The JVM's heap stores all objects created by an executing
Java program. Objects are created by Java's "new" operator,
and memory for new objects is allocated on the heap at run
time. Garbage collection is the process of automatically
freeing objects that are no longer referenced by the
program. This frees the programmer from having to keep track
of when to free allocated memory, thereby preventing many
potential bugs and headaches.

The name "garbage collection" implies that objects that are
no longer needed by the program are "garbage" and can be
thrown away. A more accurate and up-to-date metaphor might
be "memory recycling." When an object is no longer
referenced by the program, the heap space it occupies must
be recycled so that the space is available for subsequent
new objects. The garbage collector must somehow determine
which objects are no longer referenced by the program and
make available the heap space occupied by such unreferenced
objects. In the process of freeing unreferenced objects, the
garbage collector must run any finalizers of objects being
freed.

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the use of bufferedreader?

532


Why is java so important?

652


What are the different types of sorting in java?

539


What are the different types of collections in java?

530


What is the difference between jvm and jre? What is an interface?

554






Can a constructor be made final?

666


Can we have two main methods in a java class?

540


What is thread life cycle?

544


What is substring 1 in java?

591


What is navigable map in java?

526


How to disable caching on back button of the browser?

531


Why is java called the platform independent programming language?

579


What is java used for on a computer?

509


Why wait(),notify(),notifyAll() methods defined in Object class althought we are using in only threads.

2374


Why call by value prevents parameter value change?

586