Java talks about references (Strong Reference, Soft Reference, Weak Reference, Phantom Reference)

Briefly talk about references ( from the second edition of the java virtual machine  )

Signature: wander

 

 

 

1. Four kinds of references

  After JDK 1.2, Java expanded the concept of references, and divided references into four types: Strong Reference, Soft Reference, Weak Reference, and Phantom Reference. The citation strength gradually decreases.

 

2. Introduction of citations and timing of recycling

  1. Strong reference >>>  refers to references that are ubiquitous in program code, such as "Object obj=new Object()". As long as the strong reference still exists, the garbage collector will never recycle the referenced ones. object.

  2. Soft references >>>  are used to describe objects that are useful but not required. For objects associated with soft references, these objects will be listed in the reclamation range for a second reclamation before a memory overflow exception occurs in the system. If there is not enough memory for this collection, an out-of-memory exception will be thrown. After JDK 1.2, SoftReference class is provided to implement soft references.

  3. Weak reference >>>  is also used to describe non-essential objects, but its strength is weaker than soft references. Objects associated with weak references can only survive until the next garbage collection occurs. When the garbage collector works, objects that are only associated with weak references are reclaimed regardless of whether the current memory is sufficient. After JDK 1.2, the WeakReference class is provided to implement weak references. (ThreadLocal uses weak references, which can be researched by yourself)

  4. Phantom reference >>>  Phantom reference, also known as ghost reference or phantom reference, is the weakest reference relationship. Whether an object has a virtual reference will not affect its lifetime at all, and an object instance cannot be obtained through a virtual reference. The only purpose of setting a phantom reference association for an object is to receive a system notification when the object is reclaimed by the collector. After JDK 1.2, the PhantomReference class is provided to implement phantom references.

 

Guess you like

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