Java: Garbage collector

This entry is part 31 of 54 in the series Learn Java

The Java runtime environment has a garbage collector that periodically releases the memory used by objects when they are no longer needed.

This frees the programmer from the often tedious and error-prone task of memory management.

An object will be eligible for destruction when there are no more references to it. This occurs, for example, when the object goes out of scope.

An object can also be explicitly dropped by setting its references to null.

package javaapplication24;

public class Garbage {

    public static void main(String[] args) {

        Garbage g = new Garbage();


        g = null;

    public void printOut() {

        int x = 20;



Series Navigation<< Java: Default values
Java: Static >>

Leave a comment

Leave a Reply