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

Example Analysis of Java Heap memory overflow

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you an example of Java heap memory overflow analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

JAVA heap memory management is one of the main factors affecting performance.

Heap memory overflow is a very common failure in JAVA projects. Before you can solve this problem, you must first understand how JAVA heap memory works.

First, take a look at how the JAVA heap memory is divided, as shown in the figure:

1.JVM memory is divided into heap memory and non-heap memory, heap memory is divided into Young Generation and Old Generation, non-heap memory is a permanent generation (Permanent Generation).

two。 The younger generation is divided into Eden and Survivor regions. The Survivor region consists of FromSpace and ToSpace. Eden zone accounts for large capacity, Survivor two zones for small capacity, the default ratio is 8:1:1.

3. Heap memory use: objects are stored, and the garbage collector collects these objects and then collects them according to the GC algorithm.

4. Non-heap memory use: permanent generation, also known as method area, stores long-lived objects when the program is running, such as class metadata, methods, constants, properties, and so on.

In the JDK1.8 version, the permanent generation is discarded and replaced by MetaSpace, which is similar to the permanent generation, which is the implementation of the method area. The biggest difference between them is that the metaspace is not in JVM, but uses local memory.

Note that there are two parameters in metaspace:

MetaspaceSize: initializes the meta-space size and controls the GC threshold for occurrence

MaxMetaspaceSize: limit the upper limit of meta-space to prevent exceptions from taking up too much physical memory

Why remove the permanent generation?

Reason for removing the permanent generation: a change made to integrate HotSpot JVM with JRockit VM (the new JVM technology), because JRockit does not have a permanent generation.

With meta-space, there will be no permanent OOM problem!

Generational concept

The newly generated objects are first placed in the younger generation Eden area. When the Eden space is full, the Minor GC is triggered, and the surviving objects are moved to the Survivor0 area. When the Survivor0 area is full, it triggers the execution of the Minor GC,Survivor0 area to move the surviving objects to the Suvivor1 area, which ensures that there is always an empty survivor area in a period of time. Objects that are still alive after many times of Minor GC are moved to the old age.

In the old days, storing long-lived objects will trigger Major GC=Full GC,GC to stop all threads waiting for GC to complete, so applications with high response requirements should minimize the occurrence of Major GC and avoid response timeouts.

Minor GC: clean up the younger generation

Major GC: clean up the old days

Full GC: clean up the entire heap space, including the younger and permanent generations

All GC stops applying all threads.

Why the generation?

The objects are classified according to the survival probability, and the objects with long survival time are put into the fixed area, so as to reduce the scanning garbage time and GC frequency. According to the classification of different garbage collection algorithms, to enhance the strengths and avoid weaknesses of the algorithm.

Why is survivor divided into two equal pieces of survival space?

Mainly to solve fragmentation. If memory fragmentation is severe, that is, two objects occupy discontiguous memory, and the existing continuous memory is not enough for new objects to store, GC will be triggered.

Common parameters of JVM heap memory

Parameter description-Xms heap memory initial size, unit m, g-Xmx (MaxHeapSize) heap memory maximum allowable size, generally not larger than physical memory 80%-XX:PermSize non-heap memory initial size, general application setting initialization 200m, maximum 1024m is enough-XX:MaxPermSize non-heap memory maximum allowable size-XX:NewSize (- Xns) young generation memory initial size-XX:MaxNewSize (- Xmn) young generation memory maximum allowable size You can also abbreviate-the capacity ratio of Eden to Survivor in the younger generation of XX:SurvivorRatio=8. The default is 8, that is, the memory size of the 8:1-Xss stack.

Garbage collection algorithm (GC,Garbage Collection)

Red is the marked inactive object, and green is the active object.

Mark-clear (Mark-Sweep)

GC is divided into two phases, marking and clearing. First of all, all recyclable objects are marked, and all marked objects are uniformly recycled after the marking is completed. At the same time, it produces discontiguous memory fragments. Too much fragmentation will cause when the program needs to allocate larger objects later, it will not find enough contiguous memory and will have to trigger GC again.

Copy (Copy)

Divide the memory into two blocks according to capacity, using only one of them at a time. When this piece of memory is used up, copy the surviving objects to another block, and then clean up the used memory space at once. This makes it easy and efficient to reclaim half of the memory area each time, without considering the problem of memory fragmentation. The disadvantage requires twice as much memory space.

Marking-finishing (Mark-Compact)

It is also divided into two phases, first marking recyclable objects, then moving the surviving objects to one end, and then cleaning up memory beyond the boundary. This method avoids not only the fragmentation problem of the mark-removal algorithm, but also the space problem of the replication algorithm.

Generally speaking, after the younger generation executes GC, a small number of objects will survive, so the replication algorithm will be chosen, and the collection can be completed with a small amount of replication cost. In the old days, because of the high survival rate of objects and no extra memory space allocation, it was necessary to use mark-clean or mark-clean algorithms for recycling.

Garbage collector

Serial collector (Serial)

The older collector, single thread. When collecting, the worker thread of the application must be paused until the collection is finished.

Parallel collector (Parallel)

Multiple garbage collection threads work in parallel, which is more efficient under multi-core CPU, and the application thread is still in a waiting state.

CMS collector (Concurrent Mark Sweep)

The CMS collector is designed to shorten the pause application time and is implemented based on the tag-cleanup algorithm. The whole process is divided into four steps, including:

Initial tag (Initial Mark)

Concurrent tagging (Concurrent Mark)

Relabel (Remark)

Concurrent cleanup (Concurrent Sweep)

Among them, the two steps of initial marking and relabeling still need to pause the application thread. The initial tag is only to mark the objects to which GC Roots can be directly associated, which is very fast. The concurrent marking stage is to mark recyclable objects, while the re-marking phase is to correct the marking records of that part of the objects whose markup changes are caused by the continued operation of the user program during the concurrent marking period. The pause time in this stage is a little longer than that in the initial marking stage, but much longer than that in the concurrent marking period.

Because the collector thread, which consumes the longest concurrent marking and concurrent cleanup process throughout the process, can work with the user thread, the CMS collector memory collection is executed concurrently with the user, greatly reducing the pause time.

G1 Collector (Garbage First)

The G1 collector divides the heap memory into several independent regions (Region) of equal size, and can predict the pause time and the cause. It can avoid the whole heap collection. G1 tracks the value of garbage accumulation in each Region (the amount of space obtained and the time required for collection), and maintains a priority list in the background, giving priority to the collection of the most valuable Region each time according to the allowed collection time, thus ensuring higher collection efficiency in a limited time.

The work project of the G1 collector is divided into four steps, including:

Initial tag (Initial Mark)

Concurrent tagging (Concurrent Mark)

Final tag (Final Mark)

Filter Recycling (Live Data Counting and Evacuation)

The initial tag, like CMS, marks the object to which GC Roots can be directly associated. Concurrent tags mark living objects starting with GC Root, which takes a long time, but can also be executed concurrently with the application thread. The final tag is also to correct the part of the tag record that changes the tag due to the continued operation of the user program during the concurrent tag. Finally, the recovery value and cost of each Region are sorted in the screening recovery phase, and the recovery is performed according to the GC pause time expected by the user.

Garbage collector parameters

Parameter description-XX:+UseSerialGC Serial Collector-XX:+UseParallelGC parallel Collector-XX:+UseParallelGCThreads=8 parallel Collector Thread, and how many threads are garbage collected at the same time, generally equal to the number of CPU-XX:+UseParallelOldGC specifies the old age as parallel collection-XX:+UseConcMarkSweepGCCMS Collector (concurrent Collector)-XX:+UseCMSCompactAtFullCollection turns on memory space compression and defragmentation to prevent excessive memory fragmentation-XX:CMSFullGCsBeforeCompaction=0 indicates how many times FullGC begins to compress and defragment 0 means to compress and organize immediately after each Full GC-XX:CMSInitiatingOccupancyFraction=80% indicates that the old era starts to perform CMS collection when 80% of the memory space is used, to prevent excessive Full GC-XX:+UseG1GCG1 collectors-XX:MaxTenuringThreshold=0 is still alive after a few GC in the younger generation, and 0 means to enter the old age directly.

Why is there a heap memory overflow?

Objects that survive GC in the younger generation are copied to the older generation. When there is a shortage of space in the old days, JVM will conduct a complete garbage collection (Full GC) of the old years. If, after GC, objects copied from the Survivor area cannot be stored, OOM (Out of Memory) will appear.

OOM (Out of Memory) exceptions are common for the following reasons:

1) insufficient memory in the old days: java.lang.OutOfMemoryError:Javaheapspace

2) out of permanent memory: java.lang.OutOfMemoryError:PermGenspace

3) Code bug, the occupied memory cannot be reclaimed in time.

OOM may occur in all of these memory regions. When you encounter OOM, you can locate the memory overflow in which area according to the exception information.

You can add a parameter-XX:+HeapDumpOnOutMemoryError to make the virtual machine Dump the current memory heap dump snapshot in the event of a memory overflow exception for post-analysis.

Familiar with JAVA memory management mechanism and configuration parameters, the following is the tuning configuration for JAVA application startup options:

JAVA_OPTS= "- server-Xms512m-Xmx2g-XX:+UseG1GC-XX:SurvivorRatio=6-XX:MaxGCPauseMillis=400-XX:G1ReservePercent=15-XX:ParallelGCThreads=4-XX:

ConcGCThreads=1-XX:InitiatingHeapOccupancyPercent=40-XX:+PrintGCDetails-XX:+PrintGCTimeStamps-Xloggc:../logs/gc.log "

C sets the minimum and maximum values of heap memory, and the maximum value refers to the historical utilization setting

Set GC garbage collector to G1

Enable GC logging to facilitate later analysis

The above is all the contents of the article "example Analysis of Java Heap memory overflow". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report