garbage collection in Java

An object becomes eligible for garbage collection when there are no more reachable references to it.
The first way to remove a reference to an object is to set the reference variable that refers to the object to null .
We can also decouple a reference variable from an object by setting the reference variable to refer to another object.
The garbage collector has evolved to such an advanced state that it’s recommended that you never invoke System.gc() in your code - leave it to the JVM.
```
class Main {
    private static Main m;
    public static void main(String[] args) {
        Main m1 = new Main(); // m1->object
        Main m2 = new Main();
        
        m2.set(m1); //m1->object, m->object
        m1 = new Main(); //m->object
        m1 = null;
        m2.set(m1); //m->null
    }
    
    public void set(Main x) {
        m = x;
    }
}
```