Thursday, 14 September 2017

java.lang.OutOfMemoryError: GC overhead limit

Java runtime environment contains a built-in Garbage Collection (GC) process. In many other programming languages, the developers need to manually allocate and free memory regions so that the freed memory can be reused.
Java applications on the other hand only need to allocate memory. Whenever a particular space in memory is no longer used, a separate process called Garbage Collection clears the memory for them. How the GC detects that a particular part of memory is explained in more detail in the Garbage Collection Handbook, but you can trust the GC to do its job well.

The cause

The java.lang.OutOfMemoryError: GC overhead limit exceeded error is the JVM’s way of signalling that your application spends too much time doing garbage collection with too little result. By default the JVM is configured to throw this error if it spends more than 98% of the total time doing GC and when after the GC only less than 2% of the heap is recovered.
The java.lang.OutOfMemoryError: GC overhead limit exceeded error is displayed when your application has exhausted pretty much all the available memory and GC has repeatedly failed to clean it.

Example

In the following class creates a “GC overhead limit exceeded” error by initializing a Map and adding key-value pairs into the map in an unterminated loop:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package com.blogspot.informationtechnologyarchive;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class ExampleGCOverheadLimit {

 public static void main(String[] args) {
  Map<Integer,String> map = new HashMap<Integer,String>();
     Random r = new Random();
     while (true) {
       map.put(r.nextInt(), "value");
     }
 }

}
As you might guess this cannot end well. And, indeed, when you launch the above program with:
java -Xmx100m -XX:+UseParallelGC ExampleGCOverheadLimit
You soon face the java.lang.OutOfMemoryError: GC overhead limit exceeded message.

Solution

As a solution (or rather to consider it as a workaround), if you just wished to get rid of the “java.lang.OutOfMemoryError: GC overhead limit exceeded” message, adding the following to your startup scripts would achieve just that: 
 -XX:-UseGCOverheadLimit

No comments:

Post a Comment

Welcome

Hello everybody, Welcome in my blog called "Information technology archive". Obviously the topics will be related to Informatio...