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

Explain naming conventions for packages?

571


What is void data type?

566


How are the elements of a gridbaglayout organized?

537


Is Java a dying language?

562


What are different types of classloaders?

578






Is array a class?

510


How to check if a list is sorted in java?

580


how are methods defined?

547


Explain the private field modifier?

602


What is method with example?

570


How do you remove all elements from an arraylist in java?

497


What are sets in java?

498


Can inner class be public in java?

538


What are the drawbacks of reflection?

554


what is nested class in java?

594