java-virtual Machine (2)-GC algorithm & Collector

Source: Internet
Author: User

Main contents of garbage collection:

1. Which memory needs to be recycled?

2. When is it recycled?

3. How to recycle?

Garbage collection is primarily for those areas of the runtime data area?

The thread private areas of the runtime data area are: Virtual machine stack, local method stack, program counter, etc.

stack frames in the stack with the method entry and exit execution into the stack and out of the stack, the memory allocation of each stack frame in the compilation period has been determined;

As the thread or method ends, the memory is recycled as well;

The thread sharing area of the runtime data area is: Method area, heap;

The method area and the heap can only be determined when the program is running to determine which objects are created, so this part of memory allocation and recycling is dynamic;

Key areas of garbage collection;

One, the object survival judgment

1, reference counting algorithm

Adding a reference counter to an object, referencing the counter value +1, referencing the invalid counter value-1, when the counter value is 0 o'clock the object can no longer be used;

Mainstream Java virtual machines do not use this algorithm to manage memory (unresolved problems with circular references between objects)

Simple to achieve and high efficiency (application: Flashplayer,python, etc.)

2, the Accessibility analysis algorithm

The "GC Roots" object as the starting node, search down, search traversed by the path of the chain of reference, when an object to the GC Roots no reference chain, the object is not available;

Objects that can be used as "GC Roots":

"1", the object referenced by the static property in the method area

"2", the object referenced by the constant in the method area

"3", the object referenced by the virtual machine stack (local variable table in the stack frame)

"4", the object referenced by JNI in the local method stack (native method)

Second, garbage collection algorithm

1, mark-Clear algorithm

Definition: First mark the objects to be recycled, and then collect them uniformly;

Applicable: Garbage collection with many surviving objects

Disadvantages:

"1", the efficiency is low, the process of marking and cleaning is not high;

"2", space problem; After the mark is cleared, a large number of discontinuous memory fragments are generated, and there is not enough contiguous memory space to allocate memory to large objects, causing the garbage collection action to start early.

      

2, copy algorithm

Definition: Divide the available memory into two blocks of equal size, using only one piece at a time, and when this piece is exhausted, copies the surviving objects to another,

The used memory is then cleaned up once.

For: less-than-surviving garbage collection

Advantage: Each time the entire half of the memory collection, regardless of memory fragmentation problem, as long as the mobile heap top pointer, in order to allocate memory;

Easy to implement and efficient to run

Cons: Reduce memory by half

Other:

The Cenozoic memory is divided into Eden,from survivor,to Survivor three spaces according to 8:1:1, each time using Eden and from Survivor two spaces to allocate memory to the object,

When the memory is out of garbage collection, the surviving objects are copied to the to survivor space, and then the Eden and from survivor space are cleaned up, which equates to a 10% wasted memory point;

If the 10% to survivor space is not sufficient to store the surviving object, the old age is required for the allocation guarantee (the surviving object is directly into the old age through the distribution guarantee mechanism)

3, marker-collation algorithm

Definition: First mark the object to be reclaimed, move the surviving object to one end, and finally clear the memory outside the end boundary

      

4, generation of collection algorithm

Definition: The memory is divided into the Cenozoic and the old age according to the life cycle of the object, then the appropriate recovery algorithm is used according to the characteristics of each age;

such as: The new generation of surviving objects can be used in the replication algorithm; the old age has more surviving objects and no allocation guarantees. You must use the tag cleanup or mark-up recovery algorithm

Third, garbage collector

1,

 

  

java-virtual Machine (2)-GC algorithm & Collector

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.