JVM details-GC Policy

Source: Internet
Author: User

LISP, which was born in 1960 at MIT, was the first language to use the dynamic memory distribution and garbage collection technology. However, this article mainly describes the JVM Memory Management Mechanism Based on Sun hotspot.

In the memory area, the program counters, virtual machine stacks, and local method stacks have the same declaration cycle and thread as each other. When methods are executed and exited, stack frames in the stack perform inbound and outbound operations. The memory allocated for each stack frame can also be calculated in the similar structure. Therefore, the memory allocated for these memory areas is allocated, recovery is deterministic, so you don't need to worry too much about memory recovery.

Java heap and method zones because there are multiple implementations of interfaces and multiple branches of methods, only during execution can we know which objects will be created, and the allocation and recovery of this part of memory is dynamic, the garbage collector focuses on this part.

How does Java determine whether an object is alive?:

Java does not use reference notation to determine whether an object is alive. Instead, it uses the root search algorithm (C #, and lisp)

Root search algorithm:

Determine a series of GC root objects as the starting point, and search down from these nodes. The search path is referred to as reference chain ), if an object cannot be connected to GC root by referencing a chain, this object is unavailable.

GC root determination:

1. Objects referenced in the VM Stack

2. Objects referenced by class static attributes in the Method Area

3. Attributes referenced by constants in the method Area

4. native method reference objects in the local method Stack

How to recycle objects that are no longer alive in Java:

If an object is found to have no reference chain connected to GC roots after searching, it will be marked for the first time and filtered to see if it is necessary to execute the finalize () method, if the Finalize method is not overwritten or has been called by the virtual machine, the object will be deleted. Otherwise, the object will be put into a queue named F-queue, virtual opportunity automatically creates a low-priority finalizer thread to execute the Finalize method of this object, if this object has not been referenced with GC roots, it will be deleted at the next GC.

Method Area Recycling:

In hot spot, the method area is mainly divided into two parts: discarded constants and useless classes.

Recycling Waste constants: similar to recycling objects in the heap

Condition for recycling useless classes:

1. No instances of this class exist in the heap.

2. classloader for loading this class is recycled.

3. the java. Lang. Class Object corresponding to this class is not referenced (the class cannot be accessed through reflection)

In scenarios where reflection, dynamic proxy, cglib and other frameworks are widely used, as well as scenarios that frequently generate custom classloader such as JSP and osgi, the virtual machine must have the class uninstallation function. The hotspot can be controlled through the-xnoclassgc parameter.

Hotspot garbage collection Algorithm:

In the new generation, the replication algorithm is used: that is, the memory is divided into two equal parts. Each time one part is used, when the memory of one part is full, the surviving memory is copied to the other, then, all used memory is cleared.

Used in earlier years

1 mark-clear: Mark all objects to be recycled. After marking, all marked objects are recycled.

2 Mark-finishing: Mark all objects to be recycled, move all surviving objects to one end, and finally clear the memory outside the end boundary.

All collectors contained by hotspot:

Serial: Single-thread collector. By default, the new generation collector (which can be used with CMS) in client mode uses the replication algorithm, and the pause time takes precedence.

Advantage: There is no thread interaction overhead for a single CPU environment, and the highest single-thread collection efficiency can be obtained.

Disadvantage: All other worker threads must be suspended during garbage collection.

Parnew: multi-threaded version of serial. The new generation collector in server mode uses the replication algorithm and the pause time takes precedence.

Advantage: when multiple CPUs are used, the efficiency is higher than serial. By default, the number of collection threads is the number of CPUs.

Disadvantage: All other worker threads must be suspended during garbage collection.

Parallel scavenge: multi-thread, replication algorithm used, throughput used first, gctimeratio configured Throughput

Advantage: when multiple CPUs are used, the efficiency is higher than serial. By default, the number of collection threads is the number of CPUs, And the CPU utilization is high.

Disadvantage: Long Pause Time

Serial old: rarely used

Parallel old: rarely used

CMS: Based on tag-clear, with least pause time preferred


1 Initial mark-CMS initial mark (stop the world)

2 Concurrent mark-CMS concurrent mark

3 Re-mark-CMS remark (stop the world)

4 concurrent cleanup-CMS concurrent sweep

Advantages: Concurrent collection and low pause


1. CPU resources are used in the concurrent execution phase, which slows down the application

2. If the remaining memory is insufficient for the program during CMS running, concurrent mode failure-"will start serial old for garbage collection of the old generation.

3 because it is a tag-clearing algorithm, a large amount of space fragments will be generated at the end of collection. You can use-XX: + usecmscompaceatfullcollection to configure full GC for fragment sorting.

G1: Garbage first: implemented using the tag-Sorting Algorithm

By reducing the granularity of the garbage area, the entire Java heap is divided into multiple fixed-size region, and the most garbage collection area is preferentially collected according to the priority list and allowed collection time.

Allocation Policy:

In most cases, objects are preferentially allocated in the Eden area.

Large objects directly go to the old generation

Long-lived objects enter the old generation




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.