Summary of methods and principles of Java garbage collector _java

Source: Internet
Author: User
Tags garbage collection time interval

What is the Java garbage collector

The Java garbage collector is one of the three key modules of the Java Virtual Machine (JVM) (the other two are interpreters and multithreading mechanisms), providing automatic memory allocation (Memory allocation), automatic recycle (garbage Collect) capabilities for Applications, Both of these actions occur on the Java heap (a section of memory is fast). At one point, an object that has more than one reference (rreference) points to it, then the object is Alive (live), or death (Dead), as garbage, can be recycled by the garbage collector. Garbage collection operations require resources such as CPU, thread, and time, so it is easy to understand that garbage collection operations do not occur in real time (object death is immediately released), when memory is exhausted or an index is reached (Threshold, using memory for the total memory ratio column, such as 0.75), Triggers a garbage collection operation. With an exception to the death of an object, an object of type Java.lang.Thread is not reclaimed, even if it is not referenced, as long as the thread is still running.

Mechanisms for recycling

According to statistical analysis, Java (including some other high-level languages) in most of the object lifecycle is short-lived, so the Java memory Generation management. The purpose of generational is simply to use different management strategies (algorithms) for different generations of memory blocks to maximize performance. Compared with the old generation, usually younger generation is much smaller, the frequency of recovery is high, fast. In the old generation, the recovery frequency is low and time-consuming. The memory is distributed within the younger generation, and the objects in the younger generation are automatically promoted to the older generation when they are still alive through multiple recycling cycles.

Design selection (choices)

Design selection affects the implementation difficulty of the JVM garbage collector, as well as the performance metrics of the JVM, which are suitable for different scenarios. Describes the style characteristics of the recovery algorithm.

Single-threaded serial recovery VS multithreaded parallel recycling

The problem of whether the recycle operation itself is multi-threaded. The advantages of single-threaded recovery are simple, easy to implement, less fragmented, and suitable for single core machines. Multithreading in parallel recycling on multi-core machines can make full use of CPU resources, reduce recycling time, increase productivity, the disadvantage is complex and may be part of the fragments are not recycled.

Suspend application thread VS recycle and apply concurrency at recycle

The question of whether to suspend the application thread when the recycle operation occurs. The advantage of pausing the application thread is simplicity, accuracy, clean cleaning, and short time to clean up (CPU resource exclusive), the disadvantage is that the application of the thread after the suspension will cause the time of the application of the garbage collection period elongated, real-time very high system more sensitive. The advantage of recycling and application thread parallel processing is that the reaction time is more stable, the disadvantage is difficulty, high cleaning frequency and possibly fragmentation.

Non-merge freed memory fragment vs Merge-Freed memory fragment vs. to copy alive to a new place

These three selection descriptions describe how to manage the memory block fragments of death. Dead memory fragments are often scattered around the heap, and if not managed there are two problems, memory allocation due to the search for available memory caused by slow, small fragmentation will lead to waste of memory (such as large arrays require large contiguous memory fragments). There are two ways to manage, move the living memory to one end of the memory block, record the starting position of the available memory, or simply copy the living memory to a new memory area, and the original memory block is empty.

Performance index (Performance Metrics)

①, productivity (throughput)

A longer cycle (which has a long period of meaning), the ratio of the total time to the time that is not recycled. Measure the operating efficiency of the system.

②, garbage collection costs (garbage Collection overhead)

The ratio of recovery time to total time in a longer cycle. Corresponding to the production rate, add up to 100%.

③, pause interval (Pause time)

Java virtual machines when garbage collection, some algorithms suspend the execution of all application threads, some systems may be more sensitive to pause time interval.

④, Recovery frequency (Frequency of collection)

On average, the recovery operation occurs.

⑤, memory occupancy size (footprint)

such as the size of the heap.

⑥, Real Time (promptness)

How long after an object has died the memory consumed by the object is reclaimed.

Types of garbage collection

All the collector types are based on the generational technique. The Java hotspot virtual machine consists of three generations, young Generation, older generation Generation, and permanent generation (permanent Generation).

① Permanent Generation

Store classes, methods, and their descriptive information. The initial size and maximum value can be specified through the-xx:permsize=64m and-xx:maxpermsize=128m two options. Usually we do not need to adjust this parameter, the default permanent generation size is enough, but if the loaded class is very large, not enough, adjust the maximum can be.

② old age

The main storage of young generations of objects that are still alive and upgraded through multiple recycle cycles, of course, for some large memory allocations, may also be directly assigned to the permanent generation (an extreme example is that young generations simply cannot save).

③ Young Generation

The vast majority of memory allocation recovery actions occur in younger generations. As shown in the following illustration, the younger generation is divided into three regions, the original area (Eden) and two small survival areas (Survivor), and two surviving areas are divided into from and to by function. The vast majority of objects are allocated in the original area, and more than one garbage collection operation still exists in the surviving area.

Serial collector (serial Collector)

A single thread performs a recycle operation, which pauses the execution of all application threads, and the default collector in client mode, which is enforced by the-XX:+USESERIALGC command line option.

① of the younger generation (Minor Collection)

Move the Living object in the Eden area to the to area, and the to area can not fit directly to the old generation, move the from area to the to area, the to area is not fit to move directly to the old generation, from where the age is very large upgrade to the old generation. After the recycle, both Eden and the From are empty, and the From and to functions are interchanged, from the variable to,to to the From, before each round of recycling to is empty. Design of the selection for replication.

② recovery algorithm for older generation (full Collection)

The recycling of older generations is divided into three steps, marked (Mark), Cleared (Sweep), merged (Compact). The mark phase marks all surviving objects and clears the stage to release all Dead objects, merging all the living objects into the previous part of the older generation and leaving the free fragments behind. The design selection is merged to reduce the memory fragmentation.

Parallel collector (Parallel Collector)

Using multiple threads for garbage collection at the same time, the multi-core environment can fully utilize CPU resources, reduce recycling time, increase JVM productivity, the default collector in server mode. As with the serial collector, the execution of all application threads is paused during the recycle. Force the designation by-XX:+USEPARALLELGC the command line option.

① of the younger generation (Minor Collection)

Using multiple threads to recycle garbage, each thread has the same algorithm as the serial collector.

② recovery algorithm for older generation (full Collection)

The older generation is still single-threaded, the same as the serial collector.

Parallel merge collector (Parallel compacting Collection)

The collection of the young generation and the old generation is multithreading. specified by the command--xx:+useparalleloldgc option, –xx:parallelgcthreads=3 can further specify the number of threads participating in a parallel collection. As with the serial collector, the execution of all application threads is paused during the recycle. Older generations have a shorter collection time than the parallel collector, which reduces the pause interval (Pause time). Force the designation by –XX:+USEPARALLELOLDGC the command line option.

① of the younger generation (Minor Collection)

Same as parallel recycle collector (Parallel Collector)

② recovery algorithm for older generation (full Collection)

The older generation is divided into three steps, marking, counting, merging. Here we use the idea of dividing the older generation into a number of fixed-size areas (region). The mark phase, which divides all surviving objects into n groups (which should be the same as the number of recycled threads), each thread is responsible for its own group independently, marking the location of the surviving object and the survival information of the region (Region), marked as parallel. The statistical phase, which counts the survival rate of each region (Region), is in principle a higher survival rate in front of the former, where it is worth merging to find the starting position (most of the objects are not worth merging), the statistical phase is serial (single-threaded). The merging phase, which is based on the information in the statistical phase, replicates the surviving objects from one area (Region) to another (Region) in parallel.

Concurrency tag Cleanup collector (Concurrent mark-sweep Collector)

Also known as a low latency collector (low-latency Collector), the application is suspended for the shortest amount of time by various means. Basically, the application performs the recycle operation concurrently, without merging and copying operations. The command line-XX:+USECONCMARKSWEEPGC specifies that the incremental recycle mode-XX:+USECONCMARKSWEEPGC can also be specified within a single or dual-core system. Incremental recycling is the process of dividing a collection into fragments, executing a fragment, releasing the CPU resources to the application, and then, at some point in the future, the last result continues to be recycled. The aim is also to reduce latency.

① of the younger generation (Minor Collection)

Same as parallel recycle collector (Parallel Collector)

② recovery algorithm for older generation (full Collection)

It is divided into four steps, the initial mark (Initial Mark), the concurrency tag (Concurrent mark), the remark, and concurrent Cleanup (Concurrent Sweep). Note that there is no merge operation, so there will be fragmentation.

    • Initialization phase: Pauses the application thread, finds all surviving objects, takes a short time, and the collector uses a single thread.
    • Concurrent Tag phase: The Collector tag operation and the application run concurrently, and the collector uses a single-threaded token to mark the surviving object.
    • Marking again: The concurrent markup phase as the application is also running, the object may be added or modified in the process. So pause the application thread again, find all the modified objects, and use multithreaded markup.
    • Concurrent cleanup: The collector cleans and applies concurrently, and the collector uses a single thread to clean up the dead object.

Java garbage Collector's performance assessment tool

①–xx:+printgcdetails and –xx:+printgctimestamps

Information such as the start time, duration, and free memory of each generation of garbage collection.

②jmap [Options] pid

Jamp 2043 View the shared objects already loaded within the 2043 process. typically DLL files.

Jmap-heap 2043 View the memory heap configuration information and usage.

Jmap-permstat 2043 to see the loading of the permanent generation.

Jmap-histo 2043 View the load and memory footprint of the class.

③jstat [Options] pid

Jstat-class 2043 class loading, unloading, memory footprint.

JSTAT-GC 2043 GC implementation.

Postscript

Java provides automatic selection and automatic performance optimization. Before you make the garbage collector tuning, list the performance metrics that you are concerned with, tell the JVM what you are focusing on by the command line, and automatically tune the JVM, and if you're not satisfied, you can specify the garbage collector. OutOfMemory is typically due to low heap memory, which allows you to adjust the-xmx1024m and-xx:maxpermsize=128m command line options.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.