WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Core Java · Intermediate · question 30 of 100

What is a garbage collector in Java? How does it work?

📕 Buy this interview preparation book: 100 Core Java questions & answers — PDF + EPUB for $5

Garbage collection is an automatic memory management technique in Java that frees up memory that is no longer being used by the program. In Java, objects are created dynamically, and memory is allocated for them in the heap. When an object is no longer being used by the program, it becomes eligible for garbage collection.

The Java Virtual Machine (JVM) has a built-in garbage collector that periodically checks for objects that are no longer being used and frees up their memory. The garbage collector runs in the background and automatically manages the allocation and deallocation of memory.

The garbage collector works by tracing the objects that are still being used by the program and marking them as alive. All other objects are considered garbage and are eligible for collection. The garbage collector then frees up the memory occupied by the garbage objects, making it available for new objects.

The garbage collector in Java uses a few different algorithms to determine which objects are eligible for garbage collection. The most commonly used algorithm is called the mark-and-sweep algorithm. This algorithm works by starting at a set of root objects that are known to be alive, such as the objects currently referenced by the program’s variables. The algorithm then recursively traverses the object graph, marking all objects that are reachable from the root objects as alive. Once all reachable objects have been marked, the algorithm sweeps through the heap and frees up the memory occupied by the unmarked objects.

Here’s an example that demonstrates how garbage collection works in Java:

public class MyClass {
    public static void main(String[] args) {
        // create a large array of integers
        int[] numbers = new int[1000000];
    
        // set some values in the array
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = i;
        }
        
        // set the array to null, making it eligible for garbage collection
        numbers = null;
        
        // run the garbage collector
        System.gc();
    }
}

In this example, we create a large array of integers and set some values in it. We then set the array to null, making it eligible for garbage collection. Finally, we call the System.gc() method to manually trigger the garbage collector. The garbage collector will then free up the memory occupied by the array, making it available for other objects.

In summary, the garbage collector in Java is an automatic memory management technique that frees up memory that is no longer being used by the program. The garbage collector works by tracing the objects that are still being used and marking them as alive, then freeing up the memory occupied by the garbage objects. The garbage collector in Java uses a few different algorithms to determine which objects are eligible for garbage collection, with the most commonly used algorithm being the mark-and-sweep algorithm.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Core Java interview — then scores it.
📞 Practice Core Java — free 15 min
📕 Buy this interview preparation book: 100 Core Java questions & answers — PDF + EPUB for $5

All 100 Core Java questions · All topics