The point of a GC is to take the stress of resource management largely off of the user.
Objects are released when a) You exit their scope, b) All references are removed or c) (not sure if this is possible in Java) explicitly invoke the GC.
Not exactly. Firstly, A is just a special case of B. Secondly, C doesn't allow you to destroy a strongly referenced object. Lastly, that list is incomplete: there are four levels of reference strength:
* Phantom
* Weak
* Soft
* Strong
If there were only strong references, your B would be the only reason an object could become a candidate for GC. A strong reference is a direct reference; given:
Object o = new Object();
o is a strong reference to the new object, and that object can therefore not be a candidate for GC until o's scope closes, or possibly longer if the object becomes referenced by anything else.
Then you have 3 classes that built off of Refrence. Think of the reference class as such:
public abstract class Reference<T> {
T ref;
public Reference<T>(T ref) {
this.ref = ref;
}
public T get() {
return ref;
}
}
The phantom reference never stores a copy of the object:
public class PhantomReference<T> extends Reference<T> {
public PhantomReference<T>(T ref) {
super(null);
}
}
Then you have soft/weak references, which behave out of the ordinary with regards to the GC. These two types of references are only guaranteed to reference an object for as long as that object is strongly reachable. In the case of a weak reference, the object will be destroyed upon the first GC cycle. In the case of a soft reference, the GC will try to keep it around until it runs out of memory.