JVM summary--class loading/collector

Class loading:

The core of the custom class loader is to rewrite the findClass method to obtain the bytecode file.

If it is encrypted bytecode, the file needs to be decrypted in this class.

It is best not to override the loadClass method, because it is easy to break the parent delegation pattern.

 

Class.forName()和ClassLoader.loadClass()区别

  • Class.forName(): In addition to loading the .class file of the class into the jvm, it will also interpret the class and execute the static block in the class;

  • ClassLoader.loadClass(): Only do one thing, that is, load the .class file into the jvm, the content in static will not be executed, and the static block will be executed only in newInstance.

  • Class.forName(name,initialize,loader)Functions with parameters can also control whether to load static blocks. And only the newInstance() method is called by calling the constructor to create an object of the class.

 

Memory Setup Control Parameters

  • -Xms sets the minimum size of the heap.

  • -Xmx sets the maximum size of the heap.

  • -XX:NewSize sets the minimum space size of the new generation.

  • -XX:MaxNewSize sets the maximum space size of the new generation.

  • -XX:PermSize sets the minimum size of the permanent generation.

  • -XX:MaxPermSize sets the maximum size of the permanent generation.

  • -Xss sets the stack size per thread.

The parameters of the old generation are not set directly, but two parameters, the heap space size and the young generation space size, can be set for indirect control.

Old generation space size = heap space size - young generation large space size

 

Generational Collection Algorithm

The basic assumption of GC generation: the life cycle of most objects is very short and the survival time is short.

The "Generational Collection" algorithm divides the Java heap into the new generation and the old generation, so that the most appropriate collection algorithm can be adopted according to the characteristics of each generation. In the new generation, a large number of objects are found to die during each garbage collection, and only a few survive, so the replication algorithm is used, and the collection can be completed with only a small cost of copying the surviving objects. In the old age, because the object has a high survival rate and there is no extra space to allocate it, it must use the "mark-sweep" or "mark-clean" algorithm for recycling.

 

The garbage collection algorithm is the methodology of memory recycling, and the garbage collector is the specific implementation of memory recycling.

 

The CMS collector  is a collector whose goal is to obtain the shortest collection pause time.

The whole process is divided into 4 steps, including:

  • CMS initial mark "Stop The World"

  • Concurrent mark (CMS concurrent mark)

  • Remark (CMS remark) "Stop The World"

  • Concurrent sweep (CMS concurrent sweep)

The initial mark is just to mark the objects that GC Roots can directly associate with, which is very fast;

The concurrent marking phase is the process of GC Roots Tracing;

The re-marking phase is to correct the marking records of the part of the object whose marking is changed due to the continuous operation of the user program during the concurrent marking. The pause time of this phase is generally slightly longer than the initial marking phase, but it is much longer than the concurrent marking. time is short.

Since the collector thread can work with the user thread during the longest concurrent marking and concurrent clearing process in the whole process, in general, the memory recovery process of the CMS collector is performed concurrently with the user thread.

Advantages : concurrent collection, low pause
Disadvantages : generate a lot of space fragmentation, concurrent stages will reduce throughput

Parameter control:

-XX:+UseConcMarkSweepGC After using the CMS collector
-XX:+ UseCMSCompactAtFullCollection Full GC, perform a defragmentation; the defragmentation process is exclusive, which will cause the pause time to become longer
-XX:+CMSFullGCsBeforeCompaction . After performing several Full GCs, perform a defragmentation and
-XX:ParallelCMSThreads set the number of threads of the CMS (generally equal to the available CPU). quantity)

CMS is the old generation collector and the new generation collection needs to use ParNew.

 

The G1 collector has the following characteristics : (compared to the CMS collector)

  1. Space integration , the G1 collector uses the mark sorting algorithm, and will not generate memory space fragmentation. When allocating large objects, the next GC will not be triggered early because the contiguous space cannot be found.

  2. Predictable pause is another major advantage of G1. Reducing pause time is a common concern of G1 and CMS, but in addition to pursuing low pause, G1 can also establish a predictable pause time model, allowing users to specify In a time segment of length N milliseconds, the time spent on garbage collection must not exceed N milliseconds, which is almost a feature of real-time Java (RTSJ) garbage collectors.

Both the young generation and the old generation can use the G1 collector.

When the G1 collector is used, the memory layout of the Java heap is very different from other collectors. It divides the entire Java heap into multiple independent regions (Regions) of equal size. Although the concepts of the new generation and the old generation are still retained, the The new generation and the old generation are no longer a physical separation, they are a collection of part (can be discontinuous) Region.

 

https://mp.weixin.qq.com/s/sFnMxEwJiYRjwTiBIjfcZg

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326153334&siteId=291194637