• notice
  • Congratulations on the launch of the Sought Tech site

java.lang.OutOfMemoryError:GC overhead limit exceeded

java.lang.OutOfMemoryErrorThis error is a relatively classic error.After constant iteration of the JDK, the server hardware is constantly upgraded...In short, society is developing and the times are advancing.Many mistakes have disappeared in the wave of the times.I haven't seen this error for a long time, so that I think I won't encounter this error again in the Java world.As a result, I ran into TA at the most negligent time.Really, ten thousand beasts in my heart rushed past and trampled on my aging heart.

Tucao finished, get back to business.

In the early days of Java, an advantage over other languages was the memory recovery mechanism.There is no need to explicitly call the API to release the memory, and Java will automatically complete this process.This process is Garbage Collection, or GC for short.This is definitely a major benefit for programmers who are known for their laziness.However, everything has advantages and disadvantages.What is certain is that the Java language is created by humans, and GC is also human-written code, which is definitely not done automatically by a machine.In other words, the GC process is another group of programmers preset the GC conditions according to possible situations, and release the memory space that meets the recovery conditions.Once the occupied memory space does not meet the release conditions and the GC cannot clean it up, it will appear in due course java.lang.OutOfMemoryError.This error is to remind us that the programmers who write GC programs don't know how to deal with this situation, and it is inconvenient to deal with it for safety.Anyone who uses Java can solve it by himself.

Speaking of which, java.lang.OutOfMemoryErrorthere are several categories.What I encountered this time is java.lang.OutOfMemoryError: GC overhead limit exceeded, let's talk about this type of memory overflow.

Simply put, java.lang.OutOfMemoryError: GC overhead limit exceededthe reason is that there is currently no available memory, and after many GCs, the memory still cannot be effectively released.


As we all know, the GC process of JVM will be because of STW, but the pause is short so that it is not easy to perceive.java.lang.OutOfMemoryError: GC overhead limit exceededThis error is thrown when 98% of the pause time is caused by GC, but the result can only be less than 2% of the heap memory recovery.Plumbr gives a schematic diagram:

This error is actually a limitation of the balance between free memory and GC.After several GCs, only less than 2% of the memory is released, that is, very little free memory, which may be quickly filled again, which will Trigger another GC.This is a vicious circle.The CPU spends most of its time doing GC operations, and there is no time to do specific business operations.It may take a few milliseconds to complete the task in a few minutes, and the entire application is useless.


An example from Plumbr is directly given here.

class Wrapper {  public static void main(String args[]) throws Exception {    Map map = System.getProperties();    Random r = new Random();    while (true) {      map.put(r.nextInt(), "value");    }  }}

Copy code

Then set the heap memory to 100m, for example java -Xmx100m -XX:+UseParallelGC Wrapper.Different system environments may need to set different heap memory sizes, otherwise different OOM errors will occur.In fact, it is easy to understand, because java.lang.OutOfMemoryError: GC overhead limit exceededthere are two conditions: 98% of the time and 2% of the memory.If one of these two conditions is not met and the Map object is expanded as a result, java.lang.OutOfMemoryError: Java heap spacethis error may occur.


3.1 JVM parameters

JVM is given a parameter to avoid this error: -XX:-UseGCOverheadLimit.

However, this parameter does not solve the problem of insufficient memory, but only delays the error occurrence time and replaces it with java.lang.OutOfMemoryError: Java heap space.

3.2 Heap memory

Another way to be lazy is to increase the heap memory.Since the heap memory is less, just increase the heap memory.

However, this method is not a panacea.Because there may be a memory leak in the program.At this time, even if the heap memory is increased, it will be used up.

So the first two methods are only a temporary solution, not the root cause.

3.3 The ultimate method

In fact, there is an ultimate method, and it is a temporary solution, which is to find a place that takes up a lot of memory and optimize the code so that this problem will not occur.

How to find the code that needs to be optimized? It is to produce jvm snapshots through heap dump, and find objects that occupy a large amount of memory by analyzing the snapshots to find the code location.

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=heapdumpProduce a snapshot by setting parameters, and then analyze the content of the snapshot through tools such as VisualVM or MAT for positioning.Through this parameter, all the information of the heap memory when OOM occurs is written into the snapshot file, that is, if there is sensitive information in the heap memory at this time, it may cause information leakage.


Technical otaku

Sought technology together

Related Topic


Leave a Reply