Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Memory Model and garbage Collection Mechanism of JVM

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "JVM memory model and garbage collection mechanism". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

The memory model of jvm and the generational model of heap are shown in the figure above

name whether threads share content virtual machine stack no local variable table, method exit, etc. local method stack no native method program counter no byte code supported by current thread command method area yes jvm loaded class information, constants, static variables, etc. heap yes java object created

Generational reasons: improve GC performance and facilitate memory allocation

1. memory allocation

The ratio of Cenozoic to older generations is about 1:3.

The ratio of eden area to survivor area defaults to 8:1

2. memory recovery

Almost all java objects are born in the eden area. When there is not enough space in the eden area, the virtual machine will initiate a minorGC. After that, the surviving objects will be placed in the searcher area. If there is not enough space in the survivor area, they will be allocated to the old age according to the memory guarantee principle.

Waste Identification Principles: Applied Technology and Accessibility Analysis

Reference Counting: An object is recyclable if it has no references associated with it, i.e. their reference counts are not zero, indicating that the object is unlikely to be used again (but there will be circular reference problems).

Reachability analysis: Resolves the circular reference problem of reference counting. If there is no reachability path between GC roots and an object, the object is said to be unreachable. It should be noted that unreachable objects are not equivalent to recyclable objects, and unreachable objects become recyclable objects at least twice through the labeling process. If it is still recyclable after two marks, it will face recycling.

miborGC(copy-"clear-" exchange) flow: eden, ServorFrom surviving objects copy to ServorTo, age +1, 15 generations later into the old generation, large objects directly into the old generation;-"clear eden, ServorFrom;-" ServorTo and ServorFrom exchange.

Garbage collection memory guarantee principle: the continuous available memory space in the old age is greater than the total size of the new generation objects or the average size of the objects promoted to the old age will execute minorGC, otherwise it will be fullGC.

3. garbage collection algorithm

The new generation uses mark-copy algorithm to reclaim memory, theoretically requiring half of the memory to copy objects (not easy to generate memory fragments, but the available memory will be compressed by half), but 95% of the objects are born and die, so the copy area in the new generation is divided into 8:1, that is, 90% of the entire new generation memory can be used, not half.

In the old days, mark-and-purge algorithms were used to reclaim memory, resulting in discontinuous memory fragmentation.

Permanent generation (or method area) can also be recycled, that is, recycling conditions are more stringent

There are no instances of this class in java heap

ClassLoader of this class is recycled

The java.long.Class of this class is recycled, and the method cannot be changed through reflection access. If the above three conditions are met, the class in the method area will be recycled.

4. garbage collector

Garbage collector comparison name use range principle advantages and disadvantages Serial new generation single thread, copy algorithm simple and efficient, java virtual machine client mode default new generation garbage collector ParNew new new generation Serial+ multithread ParNew collector default open and CPU number of threads the same, java virtual machine running in Server mode new generation default garbage collector, ParNew garbage collector also suspends all other worker threads during garbage collection Parallel Scavenge new generation multithreaded replication algorithm focuses on programs reaching a controllable throughput Serial Old (MSC) old age single-thread tagging algorithm Client default java virtual machine default old generation garbage collector Parallel Old Multi-threaded tag cleaning algorithm Parallel Old is precisely to provide throughput priority garbage collector in the old generation. If the system has high throughput requirements, you can give priority to the new generation Parallel Scavenge and the old generation Parallel Old collector. Matching strategy CMS old multi-threaded tag cleaning algorithm

The main goal is to obtain the shortest garbage collection pause time,

The shortest garbage collection pause time can improve the user experience for more interactive programs. Overall, the CMS collector's memory collection and user threads are executed concurrently.

G1 Cenozoic/Old Age

Based on mark-defragmentation algorithm, no memory fragmentation occurs.

quiesce time can be controlled very precisely to achieve low quiesce garbage collection without sacrificing throughput

The G1 collector avoids full-area garbage collection by dividing the heap memory into separate areas of fixed size and tracking the garbage collection progress of these areas, while maintaining a priority list in the background, prioritizing the areas with the most garbage at a time based on the collection time allowed. Zoning and priority zone collection mechanisms ensure that the G1 collector can achieve maximum garbage collection efficiency in a limited time.

5. conclusion

Jvm's memory collection relies on garbage collectors, which are specific implementations of garbage collection algorithms that can be specified by startup parameters for different generations of garbage collectors. The optimization direction of all garbage collectors is to reduce the time of "stop the world" as much as possible, that is, the execution time of garbage collection threads, and increase the throughput of cpu as much as possible. Different implementations result in different garbage collectors. Such as serial, parallel, cms, no one can adapt to all garbage collection needs, are based on voluntary demand combination use, at present the most awesome is G1 recycler, but the production environment is almost not used.

"JVM memory model and garbage collection mechanism" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report