You talk with optimized jvm - illustration of garbage collection

Personal blog navigation page (click on the right link to open a personal blog): Daniel take you on technology stack 

From this we began to explore some jvm tuning problems. In jvm tuning the focus is inseparable from a garbage collection, garbage collection when the system becomes to achieve higher concurrency bottleneck, we need the necessary monitoring and regulation of the jvm If "automatic" garbage collection techniques.

This is the first article jvm to optimize series, please see the following Other:

Before tuning for, we must understand its operating principle, java garbage collection Garbage Collection often referred to as "GC", which was born in 1960, MIT Lisp language, after half a century, is now very mature. Therefore, in this chapter show from the three aspects to understand:

1. What objects need to be recycled?

2. When the recovery?

3. How to recycle?

 

First, who will be recovered

Memory is divided java virtual machine during execution of the java program will manage it for a number of different data regions, which have their respective use. Mainly includes the following components:

 

1, the program counter

The program counter the memory space occupied we can be ignored, it is a byte code executed by each thread line number indicator.

2, the virtual machine stack

java virtual machine thread stack is private, life cycle and the same thread. Memory model is the method of execution it describes. While for storing local variables, an operand stack, dynamic link, the method exports.

3, native method stacks

Native method stacks, similar to the virtual machine stack, it calls Yes native method.

4, heap

Jvm heap is the largest one in memory management. It is shared, store object instance. Also known as "gc heap." The main management area for garbage collection

5. The method area

The method area is shared memory area. It is the main type of information stored in the virtual machine has been loaded, constants, static variables, time compiler (jit) code data compiled.

 

These are the jvm main memory at run time composition, we see a common memory usage not only exist in the heap, but also exist in other areas, although the management of heap management is crucial to the program, but we can not just be limited to this an area, especially when there is a memory leak, we have in addition to the investigation of the case of heap memory, have to consider the virtual machine stack and a method area.

To know and to whom those areas of memory management, I also need to know when these areas for garbage collection.

 

Second, when recovery

 

Whether before garbage collection, we must determine one thing objects survive? This involves determining whether the object is a viable algorithm.

 

Reference counting algorithm :

Add a reference to an object counter, whenever a reference to its place, the counter + 1, when referring to the failure, the counter -1. Any time counter for the object 0 is no longer being used.

Advantages: simple, high efficiency determination, the python actionscript3 and widely used.

Cons: reference each problem can not be resolved between the objects. java not adopted

 

Reachability analysis algorithm:

By a series of objects called "GC Roots" as a starting point, to start the search downward from these nodes, called search path traversed chain of references, when an object is not connected to any reference GCRoots chain when this is demonstrated Object is unavailable.

Such as, the right side of the object is not reachable to GCRoots time, the object can be determined recyclable.

 

In java, as GCRoot objects include the following:

* Virtual Machine stack referenced object.

* The method of the object in a static property references.

* Methods zone constant reference object.

* JNI native methods referenced object.

Based on the above, we can know that when the current object is not to GCRoot up time that could be recovered will meet garbage.

It is not these objects must die, not necessarily, at this time can only sentence they exist in a kind of "probation" stage, to really declare an object of death. To go through at least two markers:

First: reachability analysis after the object was found not connected to the GCRoots, this time will be marked and screened for the first time.

Second: Object does not cover the finalize () method, or a finalize () method has been invoked through a virtual machine, this time will not be recognized as necessary to perform.

 

Third, how to recycle

 

After explaining the above two points, we probably understand, which objects will be recycled, and what is the basis for recovery, but the recovery of the job is not simple to implement, it first needs to scan all the objects, identify who can be recovered, followed by the need during the scan "stop the world" object can be frozen, or you just scan his reference information has changed, you've just nothing.

 

Generational recovery

We come from a object1 explain its recovery track in generational garbage collection algorithm.

 

1, object1 new construction, the new generation born in the Eden area.

2, minor GC, object1 still alive, moving to Fromsuvivor space, this time also the new generation.

3, minor GC, object1 still alive, this time will be, will move to ToSuv object1 area by copying algorithm, this time object1 age age + 1.

4, minor GC, object1 still alive, and this time survivor in object1 age did not reach the target of half the survivor, so in this case, the fromSuv and Tosuv area interchange by copying algorithm, surviving objects are moved to Tosuv.

5, minor GC, object1 still alive, and this time survivor in object1 age of objects has reached more than half of the survivor's (toSuv area has been filled), object1 been moved to the area years old.

6, after object1 survive for some time, this time found object1 unreachable GcRoots, but this time the old space years the ratio has exceeded the threshold, triggering majorGC (also can be considered fullGC, but the specific needs of the garbage collector to contact), this time object1 was recovered. fullGC triggers stop the world.

 

In the above Cenozoic, the age we have mentioned object, the object survive under state survivor, was not immediately promoted to the old generation objects, in order to avoid the old generation to cause too much impact, they must meet the following criteria before they can promotion:

1, after minor gc, age +1 will survive in the target area of ​​the survivor, when more than (the default) time of 15, moved to the old era.

2, dynamic objects, if the survivor space, all objects of the same age and more than half the overall size of the survivor space, grade greater than or equal to this year's target to go directly to the old era.

Using the above ideas generational garbage collection, the history of an object from survival to death experienced. During the Cenozoic time, replication algorithm will be used, in the old era, there may be used labeling - clear algorithm (mark-sweep) algorithm or mark - Collation Algorithm, these are the garbage collection algorithm based on different regions implementation, we look to achieve the principles of these types of collection algorithm.

 

Garbage collection algorithm

 

Method clear mark (Mark-Sweep)

Clear labeling method is the ideological foundation for garbage collection algorithm. Clear labeling algorithm will refuse divided into two phases: phase marking and clearance phase.

Mark phase, through the root, mark all reachable objects starting from the root, unlabeled object is not garbage object references.

Clear stage, clear all objects unmarked.

  

Replication algorithm (Copying)

Replication algorithm, the original memory space is divided into two, each with only one copy of which, when garbage collection, the memory being applicable to the live objects unused memory blocks, and then clear the memory blocks used in All of the objects.

 

Tag compression algorithm (Mark-Compact)

Mark compression algorithm is an algorithm recycling of old age.

Mark phase and mark clear consensus algorithm, for up to an object to make a mark.

Clean-up stage, in order to avoid memory fragmentation produced, all live objects compressed into one end of the memory.

 

Fourth, the garbage collector

The garbage collector is a concrete implementation of garbage collection, the garbage collector different vendors are very different, the general garbage collector will act on different generational needs to use with. The following is a combination of various garbage collector:

 

Various combinations of advantages and disadvantages:

 

The new generation GC policy

Old generation GC policy

Explanation

Combination 1

Serial

Serial Old

Serial and Serial Old are single-threaded conduct GC, characterized by the GC to suspend all application threads.

Combination 2

Serial

CMS+Serial Old

CMS (Concurrent Mark Sweep) is complicated by GC, GC thread and application threads to achieve concurrency work, you do not need to suspend all application threads. In addition, when the CMS GC fails, it will automatically use the Serial Old strategy GC.

Combination 3

ParNew

CMS

Use -XX: + UseParNewGC options open. ParNew parallel version Serial, you can specify the number of GC threads, the default number of GC threads for the number of CPU. You can use -XX: ParallelGCThreads option specifies the number of threads in the GC.

If you specify the option -XX: + UseConcMarkSweepGC option, the new generation of default ParNew GC strategy.

Combination 4

ParNew

Serial Old

Use -XX: + UseParNewGC options open. ParNew GC using the new generation of strategies, using old generation Serial Old GC policy by default.

Combination 5

Parallel Scavenge

Serial Old

Parallel Scavenge strategy is a focus on throughput controlled: application uptime / (uptime + GC application time), showing that this will make the CPU utilization as high as possible for the application to run persistent background, and not to interact more applications.

A combination of 6

Parallel Scavenge

Parallel Old

Parallel Old is a parallel version of Serial Old

 

Combined 7

G1GC

G1GC

-XX: + UnlockExperimentalVMOptions -XX: + UseG1GC # open

-XX: MaxGCPauseMillis = 50 # pause time goal

-XX: GCPauseIntervalMillis = 200 # pause interval target

-XX: + G1YoungGenSize = 512m # size of the young generation

-XX: SurvivorRatio = proportion surviving District # 6

Attached Java / C / C ++ / machine learning / Algorithms and Data Structures / front-end / Android / Python / programmer reading / single books books Daquan:

(Click on the right to open there in the dry personal blog): Technical dry Flowering
===== >> ① [Java Daniel take you on the road to advanced] << ====
===== >> ② [+ acm algorithm data structure Daniel take you on the road to advanced] << ===
===== >> ③ [database Daniel take you on the road to advanced] << == ===
===== >> ④ [Daniel Web front-end to take you on the road to advanced] << ====
===== >> ⑤ [machine learning python and Daniel take you entry to the Advanced Road] << ====
===== >> ⑥ [architect Daniel take you on the road to advanced] << =====
===== >> ⑦ [C ++ Daniel advanced to take you on the road] << ====
===== >> ⑧ [ios Daniel take you on the road to advanced] << ====
=====> > ⑨ [Web security Daniel take you on the road to advanced] << =====
===== >> ⑩ [Linux operating system and Daniel take you on the road to advanced] << = ====

There is no unearned fruits, hope you young friends, friends want to learn techniques, overcoming all obstacles in the way of the road determined to tie into technology, understand the book, and then knock on the code, understand the principle, and go practice, will It will bring you life, your job, your future a dream.

Published 47 original articles · won praise 0 · Views 272

Guess you like

Origin blog.csdn.net/weixin_41663412/article/details/104886328