Strong references, soft references, weak references, phantom references What is the difference?

In the Java language, in addition to the original data type of the variable, all other reference types are called, pointing to various objects, Java according to the length of its life cycle, reference will be divided into four categories, namely, strong, weak, references, references and soft references phantom (phantom reference), different reference types, mainly reflected a different object reachability status and the impact on garbage collection back.

Strong references

  • What is a strong reference

Object obj = new Object (), obj here is strong reference, by the new keyword to create an object reference associated with strong reference. As long as there is a strong reference point to an object, the object will be able to show that is still alive, the garbage collector will not touch such objects, when there is insufficient memory space JVM, JVM would rather throw an error (OOM) OutOfMemoryError running, the program terminates abnormally, do not rely on a strong recovery casual references "live" objects to solve the problem of insufficient memory. For an ordinary object, if no other references to the relationship, as long as more than a reference to the scope or explicitly appropriate (strong) references assigned to null, that can be garbage collected, the specific timing of the recovery will depend on garbage collection tactics

Soft references

  • What is soft references

Is a relatively strong references cited some weakening, so that the object can exempt some garbage collected only if JVM deemed insufficient memory, only to attempt to recover the soft reference object pointing.

Soft references achieved by SoftReference class. Soft references cited shorter life cycle than strong. Only when the JVM considered insufficient memory, only to attempt to recover the object soft reference points: the JVM will ensure that before throwing OutOfMemoryError, clean up the object references to the soft

  • What scenarios

Soft references are usually used to implement memory-sensitive cache. If there is free memory, it can temporarily retain the cache, clean out when memory is low, thus ensuring at the same time using the cache will not run out of memory.

Weak references

  • What is weak references

Garbage collection does not make exemption subject only to provide a reference under access to state of the object in a weak way. This can be used to build a relationship of no specific constraints, for example, to maintain a non-mandatory mappings if the objects are still trying to acquire, use it, otherwise reproduce instantiated

Weak references achieved by WeakReference class. Weak references of the life cycle is shorter than the soft references. In the process of the garbage collector thread scans memory area under its jurisdiction, once the discovery of an object with weak references, regardless of the current memory space is sufficient or not, it will reclaim its memory

  • What scenarios

Weak sensitive may be used to apply the same cache memory

Reference phantom (dummy reference)

  • What is illusion quote

Virtual reference, also known as phantom references, is achieved by PhantomReference class. You can not refer to any property or access to the object through the virtual function. Phantom references merely provide an object is to ensure that after finalize, to do certain things mechanisms. If an object holds only a phantom reference, and then it does not have any references, they are likely to be garbage collected at any time

  • What scenarios

It can be used to track objects recovered garbage collector activity, when a virtual reference before the object is associated garbage collector will receive a notification system

Welcome everyone to my blog Chou Chou, there are more details about the test of combat Oh! !

Guess you like

Origin www.cnblogs.com/zyjimmortalp/p/12639410.html