“This is the 30th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
Hello, I’m looking at the mountains.
Java. Lang. OutOfMemoryError this error is more classic mistake, after the JDK iteration, server hardware continuously upgrade… In a word, society is developing and The Times are progressing. Many mistakes have been lost in the tide of The Times. I had not seen this error for so long that I thought I would never see it again in the Java world. As a result, in the most negligent time encountered TA, is really, the heart of ten thousand god beast rushed over, ruthlessly trampled on my old heart ah.
Jokes over, back to the point.
One of the advantages Java had over other languages in its early days was the memory reclamation mechanism. Java does this automatically without explicitly calling an API to free memory, a process known as Garbage Collection, or GC. This is absolutely great news for the famously lazy apes. However, there are always pros and cons. To be sure, the Java language is human-created and GC is human-written code, not automated by a machine. In other words, the process of GC is another group of programmers according to the possible situation, preset GC conditions, to meet the conditions of memory free. Once occupied memory space is not in conformity with the conditions of release, the GC can’t clean up, that can appear in Java. Lang. OutOfMemoryError. This error is a reminder to our group of programmers that GC programmers don’t know how to handle this situation, and for safety’s sake it’s not easy to handle it. Anyone using Java can handle it on their own.
Say, Java. Lang. OutOfMemoryError several classification, the encounter is a Java. Lang. OutOfMemoryError: GC overhead limit exceeded, the following said for this type of memory.
In simple terms, the Java. Lang. OutOfMemoryError: GC overhead limit exceeded the reason is that the current has no available memory, is still not after several GCS can effectively release the memory.
Reason 1.
It is well known that THE JVM’s GC process is due to STW, but the pauses are too short to be easily sensed. Pause when caused 98% of the time is conducted in GC, but the results can only get less than 2% of the heap memory recovery, will be thrown. Java lang. OutOfMemoryError: GC overhead limit exceeded the error. Plumbr gives a schematic:
This error is a limitation in the balance between free memory and GC. After a few GC’s, less than 2% of memory is freed, which is a small amount of free memory that can be filled quickly enough to trigger another GC. This is a vicious cycle where the CPU spends most of its time doing GC and has no time for specific business operations, and a task that takes milliseconds may take minutes to complete, rendering the entire application useless.
Example 2.
I took an example from Plumbr, and I gave it right 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 the code
Then set the heap memory to 100m, such as Java -XMx100m -xx :+UseParallelGC Wrapper. Different system environments may require different heap sizes, otherwise different OOM errors will occur. Actually is understandable, because the Java lang. OutOfMemoryError: GC overhead limit exceeded requires two conditions: 98% and 2% of the time of memory. If the two conditions have a did not reach, the Map object expands, the Java may appear. Lang. OutOfMemoryError: Java heap space this mistake.
3. Solutions
3.1 the JVM parameter
The JVM gives an argument to avoid this error: -xx: -usegCoverheadLimit.
But, this parameter is not solved the problem of insufficient memory, just will delay, error occurred time and replace Java. Lang. OutOfMemoryError: Java heap space.
3.2 heap memory
Another lazy way to do this is to increase the heap. Since we have less heap memory, we can increase the heap memory.
However, this approach is not a panacea. Because there might be a memory leak in the program. At this time, even if you increase the heap memory, you will run out of time.
So the first two methods are just treating the symptoms, not the root cause.
3.3 Ultimate Method
In fact, there is an ultimate solution, and it is a palliative, is to find a large memory footprint, optimize the code, so that this problem does not occur.
How do YOU find code that needs to be optimized? Heap dump is used to produce JVM snapshots, which are analyzed to find objects with large memory footprint to find code locations.
By setting – XX: + HeapDumpOnOutOfMemoryError – XX: HeapDumpPath = heapdumps parameters to produce a snapshot, and then through the VisualVM or MAT positioning analysis tools such as snapshot content. This parameter is used to write all heap information to the snapshot file when OOM occurs, that is, if there is sensitive information in the heap at this time, it may cause information leakage.
Green hills never change, green waters always flow. See you next time.
Recommended reading
- This article describes 24 operations for Java8 Stream Collectors
- Java8 Optional 6 kinds of operations
- Use Lambda expressions to achieve super sorting functions
- Java8 Time Library (1) : Describes the time class and common apis in Java8
- Java8 time library (2) : Convert Date to LocalDate or LocalDateTime
- Java8 Time Library (3) : Start using Java8 time classes
- Java8 time library (4) : check if the date string is valid
- New features in Java8
- New features in Java9
- New features in Java10
- Optimization of access control based on nested relationships in Java11
- New features for Java11
- New features in Java12
- The journey to Java from a journeyman to an expert
Hello, I’m looking at the mountains. Swim in the code, play to enjoy life. If this article is helpful to you, please like, bookmark, follow.