Amazon Interview Question

Explain how the garbage collector works.

Interview Answers

Anonymous

Mar 23, 2011

It is the systematic recovery of heap memory, by tracking usage of references to that memory. The recovery (freeing) can occur either immediately when the last reference goes out of scope or later by other defined parameters such as when allocated space reaches a certain threshold or the system is idle. The exact functionality depends upon the implementation.

1

Anonymous

Mar 26, 2012

A generic explanation could be: 1-During the first pass GC marks in the heap the objects that are eligible to be freed, if all the references they hava are null. 2-GC deletes all marked objects in the heap. 3-The heap is compacted by the JVM to avoid memory fragmentation.

1