As we all know, Java is the JVM responsible for the allocation and recycling of memory, which is its advantage (easy to use, the program no longer like using C as the memory), but also its shortcomings (not flexible). In order to solve the problem that memory operation is not flexible, we can adopt soft reference method. In previous versions of JDK1.2, when an object was not referenced by any variable, the program could no longer use the object. That is, the program can only use the object if it is in a palpable state. 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.
Starting with the JDK1.2 version, the object's references are divided into four levels, giving the program more flexibility in controlling the object's life cycle. These four levels are high to low: strong references, soft references, weak references, and virtual references
1. Strong references
Most of the references we used before are actually strong references, which are the most common references. If an object has a strong reference, it is similar to essential necessities, and the garbage collector will never recycle it. When there is not enough memory space, the Java virtual Machine prefers to throw a outofmemoryerror error, which causes the program to terminate abnormally, and does not rely on random recycling of strongly referenced objects to resolve out-of-memory issues.
2. Soft references (SoftReference)
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. Soft references can be used to implement memory-sensitive caches.
A soft reference can be used in conjunction with a reference queue (Referencequeue), and if the object referenced by the soft reference is garbage collected, the Java Virtual machine will add the soft reference to the reference queue associated with it.
3. Weak references (WeakReference)
If an object has only weak references, it is similar to a living thing that can be used. The difference between a weak reference and a soft reference is that an object with only a weak reference has a shorter life cycle. As the garbage collector thread scans the area of memory it governs, once an object with only a weak reference is found, its memory is reclaimed, regardless of whether the current memory space is sufficient or not. However, because the garbage collector is a low-priority thread, it is not necessarily quick to discover objects that have only weak references.
A weak reference can be used in conjunction with a reference queue (Referencequeue), and if the object referenced by the weak reference is garbage collected, the Java virtual machine adds the weak reference to the reference queue associated with it.
4. Virtual Reference (Phantomreference)
"Virtual Reference", as the name implies, is a dummy, unlike several other references, a virtual reference does not determine the object's life cycle. If an object holds only virtual references, it can be garbage collected at any time, just as there are no references.
Virtual references are primarily used to track the activities of objects that are garbage collected. One difference between a virtual reference and a soft reference and a weak reference is that the virtual reference must be used in conjunction with the reference queue (Referencequeue). When the garbage collector prepares to reclaim an object, if it finds that it has a virtual reference, it will add the virtual reference to the reference queue associated with it before reclaiming the object's memory. The program can determine whether a virtual reference has been added to the reference queue to understand
Whether the referenced object will be garbage collected. If the program finds that a virtual reference has been added to the reference queue, it can take the necessary action before the memory of the referenced object is recycled.
In particular, the use of weak references and virtual references is seldom used in the programming of the century, because soft references can speed up the JVM's recovery of garbage memory, maintain the security of the system, and prevent the generation of memory overflow (OutOfMemory) problems.
Here is the code for the soft reference:
[C-sharp]View PlainCopy
- Import Java.lang. Ref. SoftReference;
- Public class Test {
- public static void Main (string[] args) {
- System. out.println ("start");
- A = new A ();
- softreference<a> sr = New softreference<a> (A);
- A = null;
- if (sr!=null) {
- A = Sr.get ();
- }
- else{
- A = new A ();
- sr = New softreference<a> (A);
- }
- System. out.println ("End");
- }
- }
- Class a{
- int[] A;
- Public A () {
- A = new int[100000000];
- }
- }
Strong references to Java objects, soft references, weak references, and virtual references