Java Garbage Collector Summary

Source: Internet
Author: User

What is a Java garbage collector?

Java garbage collector is one of the three important modules of Java Virtual Machine (JVM) (the other two are interpreters and multithreading mechanisms), providing memory for ApplicationsMemory Allocation and Garbage Collect)Function, both operations occur on the Java stack (a memory segment is fast ). At a certain point in time, if more than one reference points to an object, the object will be Live. Otherwise, the object will be treated as garbage, it can be recycled and reused by the garbage collector.Garbage collection consumes resources such as CPU, thread, and time.So it is easy to understandGarbage collection is not performed in real time.(The object is released immediately after it is killed). When the memory consumption ends or reaches a certain indicator (Threshold, the ratio of memory to total memory usage, such as 0.75), the garbage collection operation is triggered. There is an exception to the death of an object,java.lang.ThreadObjects of the type are not recycled even if they are not referenced.

Recycling Mechanism

According to statistical analysis, Java (including some other advanced languages)Most objects have a short life cycle., SoJava memory Generation Management. The purpose of generation division is to use different management policies (algorithms) for memory blocks of different generations to maximize performance. Compared with the old generation, the young generation is usually much smaller, the recovery frequency is high, and the speed is fast. The old generation has a low recovery frequency and takes a long time. In the young generation, objects in the young generation will be automatically promoted to the old generation after multiple recycling cycles.


Design Choices)

The design and selection affect the implementation difficulty of the JVM Garbage Collector and JVM performance indicators, which are suitable for different scenarios. Describes the Style Features of the recycling algorithm.

Single-thread serial recovery VS multi-thread parallel recovery

Whether the recycle operation itself is multi-threaded. The advantage of Single-thread recovery is that it is simple, easy to implement, and has few fragments. It is suitable for single-core machines. Multi-thread parallel collection can fully utilize CPU resources on multi-core machines to reduce the collection time and increase productivity. The disadvantage is that it is complicated and some fragments may not be recycled.

Pause the Application Thread while recycling VS recycle and Concurrent Application

Whether to suspend the application thread during the recycle operation. The advantage of suspending the application thread is that it is simple, accurate, clean, and cleanup time is short (the CPU resources are exclusive ), the disadvantage is that suspending the application thread will prolong the response time of the application in the garbage collection cycle, and the system with high real-time performance is sensitive. Parallel processing of recycling and application threads has the advantage that the application response time is relatively stable and the disadvantage is that the implementation is difficult, the cleaning frequency is high, and fragments may exist.

Memory segments not merged and released VS memory segments merged and released VS live copies to new places

The three models describe how to manage dead memory segments. Dead memory fragments are usually scattered in different parts of the heap. If the memory is not managed, there will be two problems. When the memory is allocated, the speed will be slow due to searching for available memory, small fragments can cause memory waste (for example, large arrays require large continuous memory segments ). There are two management methods: Move the active memory to one end of the memory block, record the starting position of the available memory, or simply copy the active memory to a new memory area, the original memory block is empty.

Performance Metrics)
  • Productivity (Throughput)
    The ratio of non-recovery time to the total time within a long cycle (only meaningful for a long cycle. Measure the system running efficiency.
  • Garbage Collection overhead)
    The ratio of the recovery time to the total time in a long cycle. In contrast to productivity, the sum is 100%.
  • Pause time)
    When Java virtual machines recycle garbage, some algorithms pause the execution of all application threads, and some systems may be sensitive to the pause interval.
  • Recycling Frequency (Frequency of collection)
    On average, how long will the recovery take place.
  • Size of memory used (Footprint)
    Such as the heap size.
  • Real-time (Promptness)
    The time after which the memory occupied by the object is recycled since the death of an object.
Garbage collection type

All recycler types are based on the generational technology. Java HotSpot virtual machines include three generations: Young Generation, Old Generation, and Permanent Generation ).

  • Permanent generation
    Storage classes, methods, and their descriptions. You can use-XX:PermSize=64mAnd-XX:MaxPermSize=128mTwo options are available to specify the initial size and maximum value. We usually do not need to adjust this parameter. The default permanent generation size is enough. However, if there are many classes to load and it is not enough, adjust the maximum value.
  • Elder Generation
    It mainly stores objects in the young generation that are still updated after multiple recycling cycles. Of course, for some large memory allocations, it may also be directly distributed to permanent generation (an extreme example is that the young generation cannot survive ).
  • Young Generation
    The vast majority of memory allocation and recycling actions occur in the young generation. As shown in, the young generation is divided into three areas: the original zone (Eden) and two small surviving zones (mirror VOR). The two surviving zones are divided into From and To by function. The vast majority of objects are allocated in the original zone. More than one garbage collection operation still exists.


Serial Collector)

A single thread executes the recycle operation. During the recycle period, the execution of all application threads is suspended. The default recycler in client mode is-XX:+UseSerialGCThe command line option is forcibly specified.

  • Minor Collection)
    Move the surviving objects in the Eden area To the To area. If the To area cannot be mounted, the objects in the From area can be moved directly To the old generation, in the From area, the age is greatly upgraded to the old generation. After the collection is complete, the Eden and From areas are empty. In this case, the functions of From and To are swapped. From is changed To, To is changed From, and To is empty before each round of collection. The design type is replication.
  • Full Collection)
    The collection of the old generation can be divided into three steps: Mark, Sweep, and Compact ). The markup phase marks all surviving objects, clears the phase, and releases all dead objects. The merging phase merges all living objects into the previous part of the old generation, leave all idle fragments to the back. The design type is merged to reduce memory fragments.
Parallel Collector)

Multiple Threads are used for garbage collection at the same time. In a multi-core environment, CPU resources can be fully utilized to reduce the collection time and increase JVM productivity. The default recycler in Server mode is used. Same as the serial recycler,Pause the execution of all application threads during the recycle process.. Pass-XX:+UseParallelGCThe command line option is forcibly specified.

  • Minor Collection)
    Multiple Threads are used to collect garbage. The algorithm of each thread is the same as that of the serial recycler.
  • Full Collection)
    The old generation is still single-threaded, the same as the serial recycler.
Parallel Compacting Collection)

The recycling of the young and old generations is done with multiple threads. Option through command-XX:+UseParallelOldGCSpecify,–XX:ParallelGCThreads=3You can also specify the number of threads involved in parallel recovery. Same as the serial recycler,Pause the execution of all application threads during the recycle process.. Compared with the parallel recycler, the collection time of the old generation is shorter, thus reducing the Pause time ). Pass–XX:+UseParallelOldGCThe command line option is forcibly specified.

  • Minor Collection)
    Same as Parallel Collector
  • Full Collection)
    The old generation consists of three steps: marking, statistics, and merging. Here we use the idea of dividing the old generation into multiple fixed-size zones (region ). Mark the stage and divide all the surviving objects into N groups (which should be the same as the number of recycled threads). Each thread is independently responsible for its own group, mark the location of the surviving object and the survival rate of the Region (Region), and mark it as parallel. In the statistical phase, the survival rate of each Region (Region) is counted. In principle, the previous survival rate is relatively high, find the start location that is worth merging (the vast majority of objects are not worth merging), and the statistical phase is serial (single thread ). In the merge phase, based on the information in the statistical phase, multiple threads concurrently copy the surviving objects from one Region to another ).
Concurrent Mark clearing recycler (Concurrent Mark-Sweep Collector)

Also known as Low-latency Collector (Low-latency Collector), the application can be suspended for the shortest time through various means.BasicThe recycle operation is performed concurrently with the application without merging or copying the operation. Use command line-XX:+UseConcMarkSweepGCYou can also specify the incremental recovery mode in a single-core or dual-core system.-XX:+UseConcMarkSweepGC. Incremental recycling refers to dividing the recycling operation into multiple segments. After executing a segment, the CPU resources are released to the application. At a certain point in the future, the previous results are recycled. The goal is to reduce latency.

  • Minor Collection)
    Same as Parallel Collector
  • Full Collection)
    There are four steps: Initial Mark, Concurrent Mark, Remark, and Concurrent Sweep ). Note that there will be fragments because there is no merge operation.
  • Initialization phase: Pause the application thread to find all the surviving objects. It takes a short time and the recycler uses a single thread.
  • Concurrent mark stage: the recycler marks the operation and concurrent operation of the application. The recycler uses a single thread to mark the surviving object.
  • Re-MARK: because the application is running in the concurrent mark stage, objects may be added or modified during this process. So againSuspend Application ThreadTo find all the modified objects and use the multi-threaded flag.
  • Concurrent cleanup: the recycler cleanup operation and concurrent application running. The recycler uses a single thread to clear dead objects.
Java Garbage Collector Performance Evaluation Tool
  • –XX:+PrintGCDetailsAnd–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 in the 2043 process. Usually DLL files.
    Jmap-heap 2043 displays the configuration and usage of the memory heap.
    Jmap-permstat 2043 to view the permanent generation loading status.
    Jmap-histo 2043 views the class loading and memory usage.
  • Jstat [options] pid
    Jstat-class 2043 class loading, detaching, and memory usage.
    Jstat-gc 2043 GC execution.
Postscript

Java provides automatic selection and automatic performance optimization functions. Before optimizing the Garbage Collector, list the performance indicators that interest you and use the command line to tell JVM the performance indicators that interest you. The JVM automatically performs the optimization. If not, you can specify a garbage collector. OutOfMemory is usually adjusted due to insufficient heap memory.-Xmx1024mAnd-XX:MaxPermSize=128mCommand Line option.

Reference

Java Memory Management Whitepaper

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.