Preface overview
In previous versions of JDK1.2, when an object was not referenced by any variable, the program could no longer use the object. This is like in daily life, after buying a certain item from the store, if it is useful, keep it, or throw it into the garbage bin and collect it from the cleaners. Generally speaking, if the item has been thrown into the dustbin, it is impossible to pick it up again.
But sometimes the situation is not so simple, you may encounter a similar chicken like the same items, food tasteless, discard. This kind of thing is useless now, keep it occupied space, but immediately throw it is not cost-effective, because it might come in handy in the future. For such dispensable items, a compromise of the treatment is: if the home space enough, first leave it at home, if the home space is not enough, even if the home of all the rubbish to remove, or can not accommodate those essential necessities of life, then throw away these dispensable items.
In view of the above problems, from jdk1.2, Java has extended the concept of reference. The program is more flexible in controlling the life cycle of objects. References are divided into strong references (stong Reference), soft references (Soft Reference), weak references (Weak Reference), virtual references (Phantom Reference). The reference strength in these 4 decreases in turn. Here we describe the concepts and how to use the references in these 4 respectively
Strong references
Most of the references used in our program are strong references, and if an object has a strong reference, it is similar to an essential item in life, and garbage goes back and never recycles it. When there is not enough memory space, the Java virtual Machine prefers to throw a outofmemoryerror error, cause the program to terminate abnormally, and not be free to reclaim a strongly referenced object to resolve the out-of-memory problem.
Object object=New object (); // Strong reference object=null; // Tell the Java virtual machine not to use this object, you can recycle the
Public void f () { Object Object=new Object (); // ...... Other code omitted }
There is a strong reference inside a method, which is stored in the stack, the real reference content (object) is saved in the heap, and when the method runs out and exits the method stack, the reference to the referenced content does not exist, and the object is recycled.
When we do not use this object explicitly to set the object to null or out of what period of the object, then the Java Virtual Machine task does not have a reference to this object, then it is possible to reclaim this object, when it is decided by the Java Virtual machine. It's like living in a place where we don't need an item, throw it in a dumpster, and when to recycle it, it's up to the people who collect the trash.
Soft references
If an object has only soft references, it is similar to a living thing that can be used. If the memory space is sufficient, the garbage collector does not reclaim it, and if the memory space is insufficient, the memory of those objects is reclaimed . The object can be used by the program as long as it is not reclaimed by the garbage collector.
Java strong references, soft references, weak references, virtual references