java object reference

After java 1.2, java has expanded the concept of reference, which is divided into: strong reference, soft reference, weak reference, and virtual reference.

 

Strong reference: It is ubiquitous in program code, similar to Object obj = new Object(); as long as the strong reference is still there, the garbage collector will never reclaim the referenced object.

 

Soft references: used to describe objects that are useful, but not necessary. For objects associated with soft references, these objects will be listed in the reclamation range and reclaimed a second time before the system will generate a memory overflow exception. The SoftRefrence class implements soft references.

 

Weak reference: It is also used to describe non-essential objects, but its strength is weaker than that of soft references. Objects associated with weak references can only survive until the next garbage collection. When the garbage collector works, objects that are only associated with weak references will be reclaimed regardless of whether the current virtual machine memory is sufficient. The WeekRefrence class implements weak references.

 

Virtual reference (also known as ghost reference or phantom reference): Whether an object has a virtual reference will not affect its lifetime at all, and it is impossible to obtain an object instance through a virtual reference. The only purpose of setting a virtual reference to an object is to receive a system notification when the object is reclaimed by the collector. The Phantomrefrence class implements phantom references.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326750289&siteId=291194637