Java Programming

Explain garbage collection?

Answer

Wiki User
01/05/2011

all classes and variables created in a program are put on something called the Heap, which is stored in main memory (RAM).

The Garbage collector gets rid of any class or variable that becomes impossible to reference ever again in the program.

For example, say you have a main method and from there you call another method, any local variables created in that other method will be put on the heap while they are in use, ie that function is running. As soon as the method ends, the Garbage collector will come and "release" the memory where those variables were for use in other parts, because you can never access them again when the method finishes.