"003" "Java Virtual Machine-object is dead"

Source: Internet
Author: User

The object is dead!

The first thing to do before the garbage collector recycles the heap is to determine which of these objects is "alive" . which are already "dead" (that is, the object that can no longer be used by any means).

1) Reference counting algorithm

Add a reference counter to each object, and whenever there is a place to reference it,  The counter value is plus l ; when the reference fails, the counter value is reduced l : At any moment the counter is 0 The object is impossible to be used again. The reference counting algorithm ( Reference counting ) is simple and efficient, and in most cases it is a good algorithm. There is no reference counting algorithm in the Java virtual machine to manage memory, the main reason is that it is difficult to solve the problem of circular reference between objects.

2) Accessibility Analysis algorithm

is through a series of called " GC Roots"object as the starting point, starting from these nodes to search down, the path traversed by the search is called the reference chain (Reference Chain) , when an object to the GC Roots It is not possible to prove that this object is not available when no reference chain is connected (in the Tuke case, from the GC Roots to the object unreachable).

can be used as The GC Roots objects include the following categories:

    • The object referenced in the virtual machine money (local variable table in the stack frame).

    • The image referenced by the class static property in the method area.

    • the object referenced by the constant in the method area. .

    • local methods in battle the native referenced by the JNI (method).

3) talk about references again

in the After JDK 1.2, Java expanded the concept of references into strong references ( strong Reference ), soft references ( Soft Reference ), weak reference ( Weak Reference ) citation, virtual reference ( Phantom Reference), their reference strength gradually weakened.

  • Strong References refers to the common in the program code. Similar to object obj = new Object (), as long as a strong reference exists, the garbage collector never reclaims objects that are referenced.

  • Soft References is used to describe objects that are useful but not necessary. The object that is associated with the soft reference. Before the system will occur with a memory overflow exception. These objects will be listed in the garbage collection scope for a second collection. If this collection still does not have enough memory, it will throw out the memory mi exception. after JDK1.2 , the Soft Reference class is provided to implement the soft reference.

  • Span style= "font-family: Song body; Font-size:16px "> weak reference JDK 1.2  , WeakReference  

  • Virtual Reference also known as Phantom Reference or phantom reference, it is the weakest reference relationship. Whether an object has a virtual reference exists, does not have an impact on its lifetime, nor does it have a virtual reference to obtain an object instance. 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 JDK 1.2 , the phantomreference class is provided to implement the virtual reference.

4) To survive or to die?

Even in the accessibility analysis algorithm is not the edge of the object, is not "not dead", to truly declare an object of death, At least two times the marker process: If the object is found to be not associated with the GC Roots  , Finalize ()   method. When the object does not overwrite the fnalize () method, or finalize () The method has been called by the virtual machine, and the virtual machine treats both cases as "not required, to be executed."

If this object is judged to be necessary to execute finalize< Span style= "font-family: Song body; Font-size:14px the ">" () method, the object will be placed in a f-queue   Finalizer thread to execute .   " Execute " means that the virtual opportunity triggers this method, But does not promise to wait for it to run over. finalize () the more objects that escape the fate of death, the last chance, later GC   will be to f-queue   The object in the second small-scale tag, if the object is not escaped at this time, it can be recycled. &NBSP;

5) Method Recycling Area

Method Area ( HotSpot virtual machine Permanent generation ) garbage collection mainly recycles two parts: obsolete constants and useless classes. Classes need to meet the following 3 conditions to be considered "useless classes":

    • All instances of this class have been recycled, that is Java  

    • load ClassLoader   for this class; has been recycled.

    • java.lang.Class   for this class; Span style= "font-family: Song body; Font-size:16px "> object is not referenced anywhere, it cannot be accessed from anywhere by reflection methods of the class

Virtual machines can reclaim useless classes that meet the above 3 criteria, but do not necessarily recycle them .


"See" "Understanding Java Virtual Machine (second edition)" "Zhou Zhiming"

"Starting" "http://my.oschina.net/u/245653/blog/388219"

"003" "Java Virtual Machine-object is dead"

Related Article

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.