JVM learning articles of -GC

Original Transfer: https://blog.csdn.net/Strong997/article/details/80033787

1、GC 

    Garbage collection Garbage Collection is often called "GC", which was born in 1960, MIT Lisp language, after half a century, is now very mature.

    jvm, the program counter, the virtual machine stack, native method stacks are born with the threads with the thread and off, with the stack frame entry and exit methods of doing push and pop operations, to achieve the automatic cleanup of memory, therefore, our memory garbage collection focuses on java heap and method area, during the program run, this part of the allocation and use of memory are dynamic.

2, survival of the subject is determined 

    Determining whether an object has survived there are two ways:

        1, reference count: each object has a reference count attribute count is incremented when a new reference count by 1 when released, the count is 0 can be recovered by reference. This method is simple, can not resolve objects referenced each other circulatory problems.

        2, reachability analysis (Reachability Analysis): Starting GC Roots down search, called search path traversed reference chain. When an object is not connected to any reference GC Roots chain, then it proves that this subject is not available. Unreachable object.

        In the Java language, GC Roots include:

            1, the virtual machine stack referenced object.

            2, the method area static property class object entity reference.

            3. The method of constant reference object region.

           4, native method stacks JNI object references.

3, garbage collection algorithm  

    3.1 mark - sweep algorithm

        "Mark - sweep" (Mark-Sweep) algorithm, as its name suggests, the algorithm is divided into "mark" and "clear" two stages: first mark all objects need to be recovered, marking the completion of a unified recovery after being out all marked objects.

        Its main disadvantages are two: one is efficiency, marking and clearance process efficiency is not high; the other is a space problem, it will produce a large number of discrete memory fragmentation mark after clearing space debris could cause too much when unable to find enough contiguous memory when the program needs to allocate a large object during subsequent runs and had to trigger another garbage collection operation in advance.

    3.2 replication algorithm

        "Copy" (Copying) collection algorithm that by the available memory capacity is divided into two equal size, uses only one of them. When this piece of memory runs out, the copy will also survive object to another one above, then memory space has been used once and then clean out.

        Such that each time a block of memory which is recovered, will not consider the complexities of memory fragmentation isochronous memory allocation, as long as the top of the stack pointer movement, in order to allocate memory, simple, efficient operation. But the cost of this algorithm is that the memory is reduced to half the original, continuous replication of long-lived objects cause a decrease in efficiency.

    3.3 mark - compression algorithm

        "Mark - finishing" (Mark-Compact) algorithm, the process is still marked with "mark - sweep" algorithm the same, but the subsequent steps are not directly recycled objects to clean up, but to all surviving objects are moved to the end, and then directly clean out the memory other than the end border

    3.4 generational collection algorithm

        "Generational collection" (Generational Collection) algorithm, the Java heap into the new generation and the old time, so that you can collect the most appropriate algorithm according to the characteristics of each era. In the new generation, each time garbage collection when there are a large number of objects found dead, only a few survive, then copy the selection algorithm, only need to pay the cost of reproduction of a small amount of live objects to complete the collection. The old era because of the high survival rate of the object, there is no extra space is allocated to its guarantee, you must use the "mark - clean-up" or "mark - finishing" algorithm to recover.
4, the garbage collector

    4.1 serial garbage collector Serial        

        Serial collector is the use of single-threaded garbage collection recovery, serial collector is only one worker thread each time recovery, concurrency for weak computer, the dedicated and exclusive serial collector often for better performance. Serial collector can be used in the new generation and the old years, depending on the role of the heap space is divided into the old and the new generation of serial collector's serial collector.

        Serial collector is the oldest of the collector, its drawback is that when Serial collectors want garbage collection, the user must suspend all processes, namely stopthe world (service pause). Until now, it is still the default virtual machine running the new generation of collectors in client mode.

        Control parameters: -XX: + UseSerialGC using serial collector    


    4.2 Parallel Garbage Collector 

        4.2.1 Parallel Garbage Collector

                Recovery is parallel improvements have been made on the basis of the serial recovery device, it can use multiple threads for garbage collection, the computer strong computing power, it can effectively shorten the actual time required for garbage collection        
        4.2.2 parallel garbage collector --ParNew

                ParNew work in the collection is a new generation of garbage collector, it is simply a serial collector multithreading, its recovery strategies and algorithms and the same serial collector. The new generation of parallel, serial years old; the new generation of replication algorithm's old mark - compression.

                Control parameters: -XX: + UseParNewGC using ParNew collector -XX: ParallelGCThreads limit the number of threads

        4.2.3 Parallel garbage collector --Parallel

                Parallel is the use of the new generation of multi-threaded algorithm to copy garbage collector, Parallel collectors are more concerned about the throughput of the system. Throughput is called a ratio of the total CPU time consumed CPU time and run user code, i.e., throughput = time to run user code / (user code running time + time garbage collection).

                The shorter the time the more suitable pause the program needs to interact with the user, a good response speed can improve the user experience;

                The high throughput can be the most efficient use of CPU time computing tasks to complete the program as quickly as possible, mainly for operations in the background without too much interaction tasks.

                Can be opened by adaptive strategy parameters, the virtual performance monitoring information collected in accordance with the operation of the current system, these parameters are dynamically adjusted to provide the optimal dwell time or a certain maximum; GC parameters can also be controlled by the time of no greater than how many milliseconds, or proportion; the new generation of replication algorithm's old mark - compression

                Control parameters: -XX: MaxGCPauseMillis set the maximum pause time garbage collection -XX: GCTimeRatio certain size setting (default 99) -XX: + UseAdaptiveSeizPolicy Open adaptive mode

        4.2.4 Parallel garbage collector --Parallel Old

                Parallel Old collector's version of the old Parallel Scavenge collector, multithreading and "mark - finishing" algorithm, is more concerned about throughput. Focus on throughput and CPU resources sensitive applications, you can give priority to add Parallel Old Parallel Scavenge collector.

                Control parameters: -XX: + UseParallelOldGC using ParallelOld collector -XX: ParallelGCThreads limit the number of threads

    4.3CMS garbage collector

        CMS (Concurrent Mark Sweep) In addition to concurrent mark Please, please mark it used to be a division, working in the old time, pause time focusing system.

        CMS is not exclusive collector, that is, the process of recovery in the CMS application is still non-stop work, and there will continue to produce new spam, so the use of the CMS process should ensure sufficient memory for the application when available, CMS applications will not wait until saturated before going to the garbage, but in a certain threshold (the default is 68) began to recover, that is to say when the execution back to the old era of space usage reaches 68% of the time CMS. If memory usage is growing rapidly, in the course of CMS implementation, there have been out of memory, this time, CMS will fail recovery, the virtual machine will start serial collector's old garbage collection, which causes the application interrupt until after the completion of garbage collection will work, this process may take longer GC pause, so the threshold is set to be set according to the actual situation.

        Shortcoming mark law is clear memory fragmentation problems, CMS offer provides some optimization settings can be set once defragmentation after the completion of CMS, you can also set how many times to defragment after CMS recycling

        Control parameters: -XX: CMSInitatingPermOccupancyFraction set threshold -XX: + UserConcMarkSweepGC use cms garbage cleaner -XX: limit the number of threads ConcGCThreads -XX: + UseCMSCompactAtFullCollection setting a defragmentation -XX after the completion of CMS: CMSFullGCsBeforeCompaction set how many times were recovered CMS after defragment

    4.4G1 (Garbage First) garbage collector

        G1 (Garbage First) garbage collector is one of the most cutting-edge technological achievements garbage collection. As early as JDK7 JVM had joined the family of collectors, garbage collection has become the focus of HotSpot technology development. With excellent CMS, like garbage collector, G1 is also concerned about the minimum delay of the garbage collector, also suitable for large-size garbage collection heap memory, the government has also recommended G1 instead select CMS. G1 biggest feature is the introduction of the idea of ​​partition, weakening the concept of generational, rational use of resources of each garbage collection cycle, solve the many limitations of other collectors or even CMS.

        Parallel and Concurrent: G1 can fully advantage of the hardware in a multi-CPU, multi-core environment, the use of multiple CPU (CPU or CPU core) to shorten the Stop-The-World's pause time, some of the other collectors need to pause the original Java thread execution GC action, G1 collector still allows concurrent Java programs by way continue.

        Generational collection: As with other collectors, generational concept is still preserved in G1. While the G1 may not be required with other collectors will be able to independently manage the entire GC heap, but it can use different ways to deal with the newly created object and has survived for some time to get through GC old object multiple times to get better collecting results.

        Spatial integration: with the CMS "mark - clean-up" of different algorithms, G1 appears to be based on the whole "mark - finishing" collector's algorithm, (between two Region) from the local point of view is based on the "Copy" algorithm in any case, these two algorithms mean no memory space debris during operation of the G1, the collection offers regular memory available. This feature is conducive to long-running programs, not because they can not find a contiguous memory space prematurely trigger the next GC when allocating large objects.

        Predictable pause: This is another advantage G1 relative to the CMS, reduce dwell time is a common concern and CMS G1, but G1 in addition to the pursuit of low pause, the pause time model can establish a predictable, allowing use by explicitly in a specified time segments of length M milliseconds, consumed in the garbage collection may not exceed N milliseconds, which is almost real Java (RTSJ) wherein the garbage collector.

        Control parameters: -XX: + UseG1GC using the G1 garbage collector -XX: limit the number of threads ParallelGCThreads -XX: MaxGCPauseMillis specify the maximum pause time

Guess you like

Origin www.cnblogs.com/123-shen/p/11346349.html