In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to understand VM tuning and generation garbage collection. Many people may not know much about it. In order to let you know more, Xiaobian summarizes the following contents for you. I hope you can gain something according to this article.
Why generations?
Generational garbage collection strategies are based on the fact that different objects have different lifecycles. Therefore, objects with different life cycles can be collected differently in order to improve recycling efficiency.
In the process of Java program running, a large number of objects will be generated, some of which are related to business information, such as Session objects, threads, Socket connections in Http requests. These objects are directly linked to the business, so the life cycle is relatively long. But there are also some objects, mainly temporary variables generated during the program running process, these objects will have a short life cycle, such as String objects, due to the characteristics of its invariant class, the system will generate a large number of these objects, some objects can even be recycled only once.
Imagine, in the case of no object lifetime distinction, each garbage collection is to collect the entire heap space, which takes a relatively long time, and at the same time, because each collection needs to traverse all the surviving objects, but in fact, for long life objects, this traversal is ineffective, because it may be traversed many times, but they still exist. Therefore, generational garbage collection adopts the idea of partition, divides generations, puts objects with different life cycles on different generations, and adopts the most suitable garbage collection method for different generations.
How to divide generations
As shown in the figure:
Virtual machines are divided into three generations: Young Generation, Old Generation, and Permanent Generation. Among them, persistent generation mainly stores Java class information, which has little to do with Java objects to be collected by garbage collection. The division of young and old generations has a greater impact on garbage collection.
Young generation:
All newly generated objects are first placed in the younger generation. The goal of the younger generation is to collect objects with short lifetimes as quickly as possible. The younger generation is divided into three zones. One Eden zone and two Survivor zones (generally speaking). Most objects are generated in the Eden zone. When Eden is full, surviving objects will be copied to Survivor (one of the two), when this Survivor is full, surviving objects in this area will be copied to the other Survivor area, when this Survivor is full, objects copied from *** Survivor areas and still alive at this time will be copied to "Tenured". It should be noted that the two sections of Survivor are symmetrical and have no relationship, so there may be objects copied from Eden and objects copied from the previous Survivor in the same section, while only objects copied from ** Survivor are copied to the old section. Also, one of the Survivor zones is always empty. At the same time, the Survivor area can be configured to be multiple (more than two) according to the needs of the program, which can increase the survival time of objects in young generations and reduce the possibility of being placed in old generations.
Older generation:
Objects that survive N garbage collections in the younger generation are placed in the older generation. Therefore, it can be considered that the old generation is stored in some objects with a longer life cycle.
Persistent generation:
Used to store static files, now Java classes, methods, etc. Persistence generation has no significant impact on garbage collection, but some applications may dynamically generate or call some classes, such as Hibernate, etc. In this case, you need to set up a relatively large persistence generation space to store these new classes during the run. The persistent generation size is set by-XX:MaxPermSize=.
Under what circumstances trigger garbage collection
Because the objects are processed in different generations, the garbage collection area and time are different. GC has two types: Scavenge GC and Full GC.
Scavenge GC
In general, when a new object is generated and Eden fails to request space, Scavenge GC is triggered to GC the Eden area, purge the non-surviving objects, and move the surviving objects to the Survivor area. Then organize the two sections of Survivor. GC in this way is performed on the Eden zone of the younger generation and does not affect the older generation. Because most objects start in Eden, and Eden is not allocated very large, GC in Eden occurs frequently. Therefore, it is generally necessary to use fast and efficient algorithms here to make Eden free as soon as possible.
Full GC
Clean up the entire heap, including Young, Tenured, and Perm. Full GC is slower than Scavenge GC because it needs to recycle the entire pair, so the number of Full GC should be reduced as much as possible. A large part of tuning the JVM is tuning FullGC. The following reasons may lead to Full GC:
Tenured is full.
Perm is full
System.gc() is displayed called
Heap's domain allocation policy changes dynamically after the last GC
Schematic diagram of generation garbage collection process
Choosing an appropriate garbage collection algorithm
serial collector
With a single thread to handle all garbage collection work, because there is no need for multi-threaded interaction, so the efficiency is relatively high. However, it is also impossible to take advantage of multiprocessors, so this collector is suitable for single-processor machines. Of course, this collector can also be used on multiprocessor machines with small data volumes (around 100M). Can be opened with-XX:+UseSerialGC.
parallel collector
Parallel garbage collection is performed on younger generations, so garbage collection time can be reduced. Generally used on multithreaded multiprocessor machines. Open with-XX:+UseParallelGC. Parallel collectors were introduced in J2SE 5.0 Update 6 and enhanced in Java SE 6.0-parallel collection of older generations is possible. If older generations do not use concurrent collection, the default is to use single-threaded garbage collection, thus limiting scalability. Open with-XX:+UseParallelOldGC.
Use-XX:ParallelGCThreads= to set the number of threads for parallel garbage collection. This value can be set equal to the number of processors in the machine.
This collector can be configured as follows:
*** Garbage collection pause: Specifies the maximum pause time during garbage collection, specified by-XX:MaxGCPauseMillis=. for milliseconds. If this value is specified, heap size and garbage collection-related parameters are adjusted to achieve the specified value. Setting this value may reduce the throughput of your application.
Throughput: throughput is the ratio of garbage collection time to non-garbage collection time, set by-XX:GCTimeRatio=, the formula is 1/(1+N). For example, -XX:GCTimeRatio=19 means 5% of the time is spent on garbage collection. The default is 99, which means 1% of the time is spent on garbage collection.
concurrent collector
It can guarantee that most of the work is concurrent (application does not stop), garbage collection only pauses for a small time, this collector is suitable for medium and large-scale applications with high response time requirements. Open with-XX:+ UseConcepMarkSweepGC.
Concurrency collectors reduce pause times for older generations by tracking reachable objects using separate garbage collection threads without stopping the application. During each age-generation garbage collection cycle, the concurrent collector briefly pauses the entire application at the beginning of collection and pauses again during collection. The second pause is slightly longer than ***, during which multiple threads do garbage collection work simultaneously.
Concurrent collectors use processors in exchange for short pauses. On an N-processor system, concurrent collection uses K/N available processors for collection, typically 1
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.