Troubleshooting: analyze outofmemoryerror exceptions
brief introduction
java. Lang. outofmemoryerror should be a very common error in Java applications.
So what is the cause of outofmemoryerror? How do we find the corresponding error? Let's have a look.
OutOfMemoryError
Let's take a look at the definition of outofmemoryerror. Outofmemoryerror inherits from virtualmachineerror. It is a kind of error. It represents an exception that cannot be handled by the application. Generally, it will cause the virtual machine to exit.
public class OutOfMemoryError extends VirtualMachineError {
@java.io.Serial
private static final long serialVersionUID = 8228564086184010517L;
/**
* Constructs an {@code OutOfMemoryError} with no detail message.
*/
public OutOfMemoryError() {
super();
}
/**
* Constructs an {@code OutOfMemoryError} with the specified
* detail message.
*
* @param s the detail message.
*/
public OutOfMemoryError(String s) {
super(s);
}
}
In general, if the heap does not have more space to allocate objects, it will throw outofmemoryerror.
Another situation is that there is not enough native memory to load Java classes.
In rare cases, if you spend a lot of time on garbage collection and release only a small amount of memory, it is also possible to cause Java lang.OutOfMemoryError。
If outofmemoryerror occurs, the corresponding stack trace information will be output at the same time.
Let's analyze different outofmemoryerrors.
java. lang.OutOfMemoryError: Java heap space
Java heap space means that new objects cannot be allocated in Java heap.
If you encounter this problem, the first solution to think of is to see if the configured heap size is too small.
Of course, if it is a program that has been running all the time, we should be vigilant when this problem suddenly occurs. Because there may be a potential memory leak. Further analysis is needed.
In another case, if the Java object implements the finalize method, the object will not be recycled immediately during garbage collection. Instead, it is placed in a finalization queue.
This queue is executed by the finalizer daemon thread. If the execution speed of the finalizer daemon thread is slower than the speed at which the object is put into the finalizer queue, the Java object can not be recycled in time.
If the application creates a high priority thread, the high priority thread may cause the object to be put into the finalization queue slower than the finalizer daemon thread.
java. lang.OutOfMemoryError: GC Overhead limit exceeded
GC overhead limit exceeded means that GC is running all the time, resulting in very slow execution of the Java program itself.
If a java program is doing GC operations 98% of the time, but only 2% of the heap space is recovered and lasts for 5 times. So Java Lang. outofmemoryerror will be thrown.
The following parameters can be used to turn off this function.
-XX:-UseGCOverheadLimit
java. lang.OutOfMemoryError: Requested array size exceeds VM limit
This error means that the array to be allocated is larger than the heap size.
For example, the maximum heap size is 256M, but this problem occurs when a 300m array is allocated.
java. lang.OutOfMemoryError: Metaspace
Since jdk8, Metaspace has been moved to Java's local memory space. If the Metaspace exceeds the size limit, Java Lang. outofmemoryerror will also be thrown.
The space size of the Metaspace can be set through maxmetaspacesize.
java. lang.OutOfMemoryError: request size bytes for reason. Out of swap space?
This exception is reported when the local heap allocation fails and the local heap is about to run out.
java. lang.OutOfMemoryError: Compressed class space
On 64 bit platforms, object pointers can be represented in 32 bits (object pointer compression).
Object pointer compression can be achieved by:
UseCompressedClassPointers
This parameter is enabled by default.
We can use compressed classspacesize to set the size of pointer compression space.
OutOfMemoryError: reason stack_ trace_ with_ native_ method
This error indicates that the local method encountered an allocation failure.
This problem may need to be solved by the local debugging tool of the operating system.