Java Virtual machine-garbage collector

Source: Internet
Author: User

The birth of a garbage collector (garbage Collection, GC) led to three questions:

What memory needs to be recycled?

When do I recycle?

How to recycle?

The three regions (program counters, virtual machine stacks, local method stacks) that are exclusive to threads do not have to worry too much about garbage collection, as they are created with threads and disappear as threads end. However, the Java heap and the method area are different, one interface of multiple implementation classes need memory may be different, a method of multiple branches need not the same memory, we only know when the program is running to create which objects, this part of the memory allocation is dynamic, so this is the aspect of the GC's attention.

  How to tell if an object is dead

  (1) Reference counting method: Add a reference counter to the object, whenever a place refers to it, add one, the reference fails, minus 1. The reference counting method is simple, the decision efficiency is also high, but often there is such a situation, two objects quoted each other, in addition to no other use, so it is not considered garbage object? After being verified, such objects are also recycled by GC, so we can determine that the Java Virtual machine GC is not using this mechanism.

(2) Accessibility analysis method: The basic idea of this algorithm is to use a series of objects called "GC Roots" as the starting point, starting from these nodes to search down, search traversed by the path called the reference chain. When an object does not have any reference chain connected, we can get this object is not available, so this object will be judged to be recyclable objects.

  To survive or to die?

An object that cannot be reached in the accessibility analysis, nor is it an "undead" object, when they are temporarily in a "suspended" state, to truly proclaim the death of an object, at least two marking processes: If the object is unreachable, it will be marked for the first time and filtered, The criteria for filtering is whether this object is necessary to execute the Finalize () method, and if (1) No such method (2) has been implemented, then it can be considered "no need to execute". If the object is judged to be necessary to execute the Finalize method, the object will be placed in a queue called F-queue and then executed by a virtual machine automatically established, low priority finalize thread. If the object saves itself in this process, and the other establishes a connection, it is removed from the collection that is about to be recycled. If there is no connection, it will be really recycled.

  

  

Java Virtual machine-garbage collector

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.