Deep understanding of Java Virtual Machine notes---Reference types and objects are dead

Source: Internet
Author: User

Prior to JDK1.2, references in Java were traditionally defined: if the value of the reference type represents the starting address of another piece of memory, it is called the memory represents a reference. This definition is pure, but too narrow, an object in this definition is only referenced or not referenced in two states, for how to describe a "tasteless, discard" object is powerless, if there is garbage collection within the very tense, you can discard these objects. Many of the system's caching capabilities are consistent with this scenario.
After JDK1.2, Java extends the concept of references into strong references (strong Reference), soft references (Soft Reference), weak references (Weak Reference), virtual references (Phantom Reference ) of four, these four reference strength dependence gradually weakened.

1. A strong reference is a common reference in program code, such as "Object obj = new Object ()", as long as a strong reference exists, the garbage collector never reclaims the referenced object.
2. Soft references are used to describe some objects that are also useful, but not necessary. For objects that are associated with soft references, these objects are listed in the collection scope and recycled a second time before the system is about to have a memory overflow. If this collection still does not have enough memory, a memory overflow error will be thrown. After JDK1.2, a softreference is provided to implement the soft reference.
3. A weak reference is also used to describe a non-mandatory object, but its strength is weaker than a soft reference, and the object associated with the weak reference only survives until the next garbage collection. When the garbage collector is working, the object associated with the weak reference is reclaimed regardless of whether the current memory is sufficient. After JDK1.2, a weakreference is provided to implement a weak reference.
4. Virtual reference It is the weakest reference relationship. Whether an object has a virtual reference exists, does not have an impact on its lifetime, and cannot obtain an instance of an object through a virtual reference. The only purpose of setting a virtual reference association for an object is to expect to receive a system notification when the object is reclaimed by the collector. After JDK1.2, a phantomreference is provided to implement the virtual reference.


Objects that are unreachable in the root search algorithm are not "dead", and they are temporarily in the "probation" stage to actually declare an object to die, at least two times to go through the tagging process: If the object finds no reference chain connected to the GC roots after the root search, Then it will be marked for the first time and filtered for the condition that it is necessary for this object to execute the Finalize () method. When the object does not overwrite the Finallize () method or the object's Finalize () has been called, the virtual machine treats both cases as "no need to execute."
If an object is judged to be necessary to execute the Finalize () method, then the object will be placed in a queue named F-queue and executed by a low-priority finalizer thread that is automatically established by a virtual machine later. The so-called "execution" here refers to the virtual opportunity to trigger this method, but does not promise to wait for it to run over. The reason for this is that if an object executes slowly in the Fianlize () method, or if a dead loop occurs, it is likely to cause other objects in the F-queue queue to be permanently waiting, or even to crash the entire memory-recycling system. The Finalize () method is the last chance for an object to escape the fate of death, and later the GC will make a second small-scale mark on the object in F-queue, if the object succeeds in finalizing () saving itself-as long as it is re-associated with any object on the reference chain. It will be removed from the collection of "going to be recycled" at the second mark, and it will be recycled if the object has not escaped at this time.

Deep understanding of Java Virtual Machine notes---Reference types and objects are dead

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.