How to mark an object for garbage collection by the GC (Garbage Collector)?

You heard wrong, but then again the description is wrong too.

You don't set an object to null, you set a variable to null. If the variable can be used to get to an object, then the variable has a reference to the object. Setting the variable to null is the same as the variable "losing the reference" to the object.

Once Java detects that an object, or a group of objects can't be reached by the running program, it will remove those objects from memory. It won't remove them from memory one moment sooner, because if it did, and some other part of the program tried to use a reference to the object, then the reference would fail in a way that's not permissible in Java.

The trick is not to set just one reference to null, you have to set all the references that might have been made to null. That's why it's important to consider each time you create a new reference, because you want to create them in such a manner that they will eventually be cleared (unless you want a memory leak).


I know this question has been answered, but there are ways you can manipulate the way that the Java Garbage collector looks at your references. You can do this through Soft References, Weak References, and Phantom References. Take a look at the java.lang.ref package for a better explanation.

Also here's a nice sample to determine when a PhantomReference is about to be garbage collected:

A phantom reference is used to determine when an object is just about to be reclaimed. Phantom references are safer to use than finalization because once an object is phantom reachable, it cannot be resurrected.

// Create the phantom reference.
ReferenceQueue rq = new ReferenceQueue();
PhantomReference pr = new PhantomReference(object, rq);

// Wait until the object is about to be reclaimed.
try {
    while (true) {
        Reference r = rq.remove();
        if (r == pr) {
            // The object is about to be reclaimed.
            // Clear the referent so that it can be reclaimed.
            r.clear();
        }
    }
} catch (InterruptedException e) {
}

The Java Garbage Collector works with a mark and sweep method. This means from object that are known to still be in used all references are followed and the objects that are visited in that way are marked. In that way objects with no references at all are not marked and should be sure to be deleted. So you could ensure that all references to this object are removed, so that in the next turn of the Garbage Collector, the item is deleted.

Additionally you could use

Runtime.getRuntime().gc();

to indicate that the Garbage Collector should run. Note: you can't be sure that it really runs.


No, you can't. What would you expect to happen if another variable had a reference to it?

Note that you can't set an object to null - you can only set a variable to null. If another variable still has a reference to the object, it will still not be eligible for garbage collection.

If you think you need to do this, that probably means you're misinterpreting your data - or that you may have a leak somewhere in your code (e.g. a list which you only ever add entries to, referenced by a static variable - those entries will never be eligible for garbage collection while the classloader is alive).

Each JVM has its own GC, but in Hotspot an object will be garbage collected next time the GC runs over the generation that object currently "lives" in (assuming it doesn't have a finalizer, which complicates things). If the object is in a "young" generation, that will probably happen quite soon - if it's in an "old" generation it may well take longer.

You may want to see the Java 6 GC tuning documentation for more information, although of course things have moved on since then for OpenJDK 7 etc.