Starting with this article, we’ll explore some JVM tuning issues. Garbage collection is one of the most important aspects of JVM tuning, and when garbage collection becomes a bottleneck for systems to achieve higher concurrency, the necessary monitoring and tuning of “automated” garbage collection techniques in the JVM is required.


Before tuning, it is important to understand how it works. Java’s Garbage Collection is often referred to as “GC”. It was created in 1960 by MIT’s Lisp language and is now mature after more than half a century. Therefore, this article mainly from the three aspects:
  • 1. Which objects need to be reclaimed?
  • 2. When do you recycle?
  • 3. How to recycle?



One, who will be recycled



During the execution of Java programs, the Memory managed by the Java VIRTUAL machine is divided into several different data areas, which have their own purposes. It mainly consists of the following parts:







1. Program counter
The memory footprint of the program counter is negligible, and it is a line number indicator of the bytecode executed by each thread.


2. Virtual machine stack
Java’s virtual machine stack is thread-private and has the same lifecycle as a thread. It describes the in-memory model of method execution. It is also used to store local variables, operand stacks, dynamic links, method exits, etc.


3. Local method stack
A native method stack, similar to a virtual machine stack, calls native methods.


4, heap
The heap is the largest chunk of managed memory in the JVM. It is shared and holds object instances. Also known as “GC heap”. The primary management area for garbage collection


5. Method area
The method area is also a shared memory area. It mainly stores class information that has been loaded by the virtual machine, constants, static variables, and jit compiled code data.


Above is the JVM of main memory at run time, we see the common memory usage not only exists in the heap, also exists in other areas, while the heap management is very important for the management of the program, but we should not only limited to this area, especially when there is a memory leak, we in addition to screening heap memory, You also need to consider the virtual machine stack and method area.


Knowing who and which areas to manage memory for, I also need to know when to garbage collect those areas.


Two, when to recycle



One of the things we must determine before garbage collection is whether the object is alive? This is where the algorithm comes in to determine whether the object is alive or not.


Reference counting algorithm:



Add a reference counter to the object, which is +1 every time a reference is made to it, and -1 when the reference fails. An object whose counter is 0 at any point in time cannot be used again.


  • Advantages: simple implementation, efficient judgment, widely used in ActionScript3 and Python.
  • Disadvantages: Cannot solve the problem of objects referring to each other. Java was not adopted


Accessibility analysis algorithm:



A series of objects called “GCRoots” are used as starting points to search down from these nodes. The search path is called the reference chain. When an object is not connected to GCRoots by any reference chain, it is proved that the object is not available.
For example, the object on the right is unreachable when it goes to GCRoot and can be considered recyclable.





In Java, objects that can be GCRoot include the following:
  • * Objects referenced in the virtual machine stack.
  • * Objects referenced by static properties in the method area.
  • * The object referenced by the constant in the method area.
  • * Objects referenced by JNI in local methods.




Based on the above, we know that when the current object is unreachable in GCRoot, the possibility of garbage collection is met.


It is not certain, then, that these objects must die, but that they can only be condemned to exist in a kind of “reprieve” phase, and that an object is really declared dead. Go through at least two marks:
First time: After the reachability analysis, the object is not connected to GCRoots and is marked and filtered for the first time.
The second time: The object does not overwrite finalize (), or finalize () has already been called by the virtual machine, it will be considered as unnecessary to execute.



Three, how to recycle



After the above two explanations, we probably understand, what objects will be recycled, and the recycling according to what is, but this is not a simple work to implement recycling, first of all, it needs to scan all the object, to identify who can be recycled, the second during the scan need to “stop the world” objects can be frozen, or you just scan, If his quotes change, it’s all for nothing.


Generational recycling



We illustrate its collection trajectory in generational garbage collection algorithms from an OBJECT1.


1. Object1 New-build, born in Eden area of the new generation.

2, Minor GC, Object1 is still alive and moved to Fromsuvivor space, which is still in Cenozoic.

3, Minor GC, Object1 is still alive, and the copy algorithm will be used to move object1 to the ToSuv region, where object1’s age+1.

4. In minor GC, Object1 still survives. At this time, the number of survivor and object1 of the same age has not reached half of survivor, so at this time, the fromSuv and Tosuv areas are swapped by replication algorithm, and the surviving object is moved to Tosuv.



5, Minor GC, Object1 is still alive, with more than half of survivor and Object1’s peers having reached survivor (the toSuv area is full), and object1 is moved to the old age area.


6. After object1 has survived for a period of time, object1 is found to be unreachable to GcRoots, and the old age space ratio has exceeded the threshold. MajorGC (fullGC) is triggered, and Object1 is reclaimed. FullGC triggers Stop the World.






In the above new generation, we have mentioned the age of the object. The object is in survivor state and will not be promoted to the old generation immediately, so as to avoid too much influence on the old generation. They can be promoted only when they meet the following conditions:
  • 1. After minor GC, the age of an object in a survivor region is +1, and when it exceeds (by default) 15, it is moved to the old age.
  • 2. Dynamic objects. If the sum of the size of all objects of the same age in survivor space is more than half of that in survivor space, the objects of grade greater than or equal to that grade can enter the old age directly.



The above uses the idea of generational garbage collection, the journey of an object from life to death. In the middle, in the new generation, there will be a copy algorithm, and in the old generation, there will be a Mark-sweep algorithm or a mark-sweep algorithm, and these are garbage collection algorithms based on different regions, so let’s look at how they work.


Garbage collection algorithm



Mark-sweep


Tag cleanup is the ideological basis of garbage collection algorithm. The tag removal algorithm divides garbage into two phases: the tag phase and the cleanup phase.
Mark stage, through the root node, mark all reachable objects from the root node. Unmarked objects are unreferenced junk objects.
The clear phase clears all unmarked objects.





Copying algorithms


The replication algorithm is to divide the original memory space into two pieces and use only one piece at a time. During garbage collection, the living objects in the applicable memory are copied to the unused memory block, and then all objects in the used memory block are cleared.




Mark Compression Algorithm (Mark-Compact)


The label compression algorithm is an old collection algorithm.
The marking stage is consistent with the marking clearing algorithm, marking the reachable object once.
The cleanup phase compresses all living objects to one end of memory in order to avoid memory fragmentation.


Garbage collector

Garbage collector is the specific implementation of memory reclamation. Garbage collectors provided by different vendors vary greatly. General garbage collectors will act on different generations and need to be used together. Here is how the various garbage collectors are combined:





Advantages and disadvantages of various combinations:


New Generation GC strategy
Generation GC policy
instructions
Mix 1
Serial
Serial Old
Serial and Serial Old are single-thread GC and are characterized by suspending all application threads during GC.
Combination of two
Serial
CMS+Serial Old
CMS (Concurrent Mark Sweep) is a Concurrent GC that enables GC threads and application threads to work concurrently without suspending all application threads. In addition, when the CMS fails to perform GC, the Serial Old policy is automatically used for GC.
Group 3
ParNew
CMS
Use the -xx :+UseParNewGC option to enable it. ParNew is a parallel version of Serial, and you can specify the number of GC threads, which by default is the number of cpus. You can specify the number of GC threads using the -xx :ParallelGCThreads option.
If the -xx :+UseConcMarkSweepGC option is specified, the new generation uses the ParNew GC policy by default.
Combination of four
ParNew
Serial Old
Use the -xx :+UseParNewGC option to enable it. The new generation uses the ParNew GC policy, and the Old generation uses the Serial Old GC policy by default.
Combination of five
Parallel Scavenge
Serial Old
The Parallel Insane strategy is primarily focused on a controlled throughput: application run time/(application run time + GC time) so that CPU utilization is as high as possible and is appropriate for persistent applications in the background and not for applications with a lot of interaction.
Combination of 6
Parallel Scavenge
Parallel Old
Parallel Old is a Parallel version of Serial Old
Combination of 7
G1GC
G1GC
+ UnlockExperimentalVMOptions – – XX: XX: + UseG1GC # open
-xx :MaxGCPauseMillis =50 # Pause time target
-xx :GCPauseIntervalMillis =200 # Pause interval target
-xx :+G1YoungGenSize= 512M # Young generation size
-xx :SurvivorRatio=6 # SurvivorRatio


The above advantages and disadvantages from: www.importnew.com/23752.html


Focus on game research and development, committed to promoting the progress of the domestic game community, welcome to follow my official account: Cool_wier