Garbage collector and memory allocation policy

Source: Internet
Author: User

Garbage collector and memory allocation policy

One, GC Collector

1. GC recycling three things to think about:

A) How to determine which memory needs to be recycled?

b) What recycling is used?

c) when to recycle?

The following is a description of each of these issues.

Question 1: How can I tell what memory needs to be recycled?

There are two methods of judging (hotspot defaults to the second type):

1. Reference counting algorithm

Add a reference counter to the object, and each time a place references it, the counter value is incremented by 1, and when the reference fails, the counter value is reduced by 1, and any object with a counter of 0 at any time is impossible to use again.

2. Root Search algorithm

With the "GC Roots" object as the starting point, starting from these nodes, the search path is referred to as the reference heap, proving that the object is not available when an object is connected to the gcroots without any reference chain. There are several reference objects in Java that can be used as GC root objects:

The object referenced in the virtual machine stack;

The object referenced by the class static property in the method area;

The object referenced by the constant in the method area;

The object referenced by the native method;

Note: 1) the Finalize () method for each object is automatically called only once by the system.

2) objects that cannot be reached in the root search algorithm are not "dead". This type of object can be self-saving, and when the Finalize () method is called, it is re-associated with any object on the reference chain. But if you face the next collection of objects, you will not be able to save yourself.

Question 2: What is recycled?

Garbage collector is used for garbage collection based on garbage collector.

The garbage collection algorithm is described below:

    1. Tag-purge algorithm (most basic algorithm, suitable for the old age)

Principle: The algorithm is divided into two tags and clear two stages, first mark (Root search algorithm) out all the objects that need to be recycled, after the mark is complete, the unified collection of all tagged objects.

Cons: Efficiency problems, the efficiency of marking and cleaning process is not high, space problem, the mark after the purge will produce a lot of fragmentation, to the future running program can not allocate continuous memory.

2. Copy algorithm (for new generation)

Principle: The available memory is divided into two blocks of equal size, one at a time, when this piece of memory is used up, the surviving objects are copied to the other, and then the used memory space is cleaned out once.

    1. Tag-Collation algorithm (for the old age)

The tagging process is based on the tag-purge algorithm, and the next step is to have all surviving objects move toward one end and then directly clean out the memory outside the end boundary.

Next, we introduce the garbage collector:

1. Serial collector (copy algorithm): The oldest collector. Feature: Single-threaded collector, when garbage collection is performed, all other worker threads must be paused until it is collected at the end (STW). Choosing the serial collector in a virtual machine in client mode is a good choice.

2. Parnew collector (copy algorithm): Multi-threaded version of the serial collector. is the preferred Cenozoic collector for many virtual machines running in server mode.

3. Parallel scanvenge Collector (copy algorithm): Parallel multi-threaded collector, high throughput (run code time/Run code time + garbage collection time).

4. Serial old collector (tag grooming)

5. Parallel old collector (marker grooming algorithm): primarily combined with Parallel scanvenge collectors.

6. CMS Collector (Mark Clear method)

Second, memory allocation and recovery strategy

1. The object takes precedence over Eden allocation; when there is not enough space in the Eden area to allocate, the virtual machine will initiate a MINORGC (Cenozoic GC).

2, large objects directly into the old age. (parameter setting allows allocation of the maximum memory size of the object)

3, long-term survival of the object will enter the old age. (default age 15, can also be set)

4, dynamic Age judgment: if the size of all objects of the same age in the survivor space is greater than half of the survivor space, objects older than or equal to that age can enter the old age directly (not restricted by parameters)

5. Space Allocation Guarantee

When Minor GC, the VM detects whether the average size of each promotion is greater than the remaining space size of the old age, and if it is greater, it is changed directly to full GC. If it is less than, check whether the guarantee is allowed, minor GC if allowed, and full GC if not allowed. (Question 3: When is it recycled?) )

Garbage collector and memory allocation policy

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.