Strong reference, soft reference, weak reference and virtual reference of java object


Label: javaoutofmemoryerror life virtual machine jvmjdk
2011-05-05 21:00 30557 people read comments (4) Collection report
Category  : JAVA (81)
Copyright statement: This article is an original article by the blogger, and may not be reproduced without the blogger's permission.

     As we all know, in java, the JVM is responsible for memory allocation and recycling. This is its advantage (convenient to use, and the program no longer has to worry about memory like using c), but it is also its disadvantage (not flexible enough). In order to solve the problem of inflexible memory operation, methods such as soft references can be used.



     In versions prior to 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 reachable state. It's like in everyday life, after buying something from the store, if it's useful, keep it, otherwise throw it in the trash to be collected by the cleaners. Generally speaking, if an item has already been thrown in the trash, it is impossible to retrieve it for use.


    But sometimes the situation is not so simple. You may encounter items like tasteless ribs, which are tasteless and a pity to discard. This item is useless now, and it will take up space to keep it, but it's not worthwhile to throw it away right now, because it might come in handy in the future. For such dispensable items, a compromise approach is: if there is enough space in the home, keep it at home first, if there is not enough space in the home, even if all the garbage in the house is removed, it still cannot accommodate the necessary items. If you have less daily necessities, then throw away these dispensable items.

    Since JDK1.2 version, the reference of objects is divided into four levels, so that the program can control the life cycle of objects more flexibly. The four levels from high to low are: strong reference, soft reference, weak reference and virtual reference.

   

1. strong citation



     Most of the citations we used before were actually strong citations, which are the most commonly used citations. If an object has a strong reference, it's like an essential household item, and the garbage collector will never recycle it. When the memory space is insufficient, the Java virtual machine would rather throw an OutOfMemoryError error to cause the program to terminate abnormally, rather than solve the problem of insufficient memory by arbitrarily recycling objects with strong references.


2. Soft Reference (SoftReference)

    If an object has only soft references, it is similar to the necessities of life. If there is enough memory space, the garbage collector will not reclaim it, if there is not enough memory space, the memory of these objects will be reclaimed. The object can be used by the program as long as it is not collected by the garbage collector. Soft references can be used to implement memory-sensitive caches.
   Soft references can be used in conjunction with a reference queue (ReferenceQueue). If the object referenced by the soft reference is garbage collected, the JAVA virtual machine will add the soft reference to the associated reference queue.



3. Weak reference (WeakReference)
    If an object has only weak references, it is similar to the necessities of life. The difference between weak references and soft references is that objects with only weak references have a shorter lifetime. In the process of scanning the memory area under the jurisdiction of the garbage collector thread, once an object with only weak references is found, its memory will be reclaimed regardless of whether the current memory space is sufficient or not. However, since the garbage collector is a low-priority thread, objects that only have weak references may not be discovered quickly.
    Weak references can be used in conjunction with a reference queue (ReferenceQueue). If the object referenced by the weak reference is garbage collected, the Java virtual machine will add the weak reference to the associated reference queue.


4. PhantomReference
    "Virtual reference", as the name implies, is just in name only. Unlike other references, virtual reference does not determine the life cycle of an object. If an object only holds phantom references, then it can be garbage collected at any time as if it had no references at all.
    Phantom references are mainly used to track the activity of objects being garbage collected. One difference between virtual references and soft references and weak references is that virtual references must be used in conjunction with reference queues. When the garbage collector is ready to reclaim an object, if it finds that it still has a virtual reference, it will add the virtual reference to the reference queue associated with it before reclaiming the memory of the object.

    The program can know whether the referenced object is about to be garbage collected by judging whether a virtual reference has been added to the reference queue . If the program finds that a virtual reference has been added to the reference queue, it can take necessary action before the memory of the referenced object is reclaimed.



    Pay special attention to the fact that weak references and virtual references are rarely used in Century programming, and soft references are often used. This is because soft references can speed up the JVM's recovery of garbage memory, maintain the operating security of the system, and prevent memory usage. Problems such as overflow (OutOfMemory) are generated.

    The following is the code for the soft reference:

[c-sharp] view plain copy
import java.lang.ref.SoftReference; 
public class Test { 
     
    public static void main(String[] args){ 
        System.out.println("start"); 
         
        A 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]; 
    } 





Reference article: http://java.chinaitlab.com/oop/716371.html

"java optimization programming 》

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326489869&siteId=291194637