Introduction to the

Java. Lang. OutOfMemoryError Java applications should be a very common mistake.

So what is the cause of OutOfMemoryError? How do we find the corresponding error? Take a look.

OutOfMemoryError

Take a look at the definition of OutOfMemoryError. OutOfMemoryError is derived from VirtualMachineError. OutOfMemoryError is a type of Error.

public class OutOfMemoryError extends VirtualMachineError {
    @java.io.Serial
    private static final long serialVersionUID = 8228564086184010517L;

    /**
     * Constructs an {@code OutOfMemoryError} with no detail message.
     */
    public OutOfMemoryError(a) {
        super(a); }/**
     * Constructs an {@code OutOfMemoryError} with the specified
     * detail message.
     *
     * @param   s   the detail message.
     */
    public OutOfMemoryError(String s) {
        super(s); }}Copy the code

In general, OutOfMemoryError is thrown if there is no more space on the heap to allocate objects.

There is also a case where there is not enough native memory to load a Java class.

In rare cases, if spend a lot of time for recycling and released only a few memory, is also likely to trigger a Java lang. OutOfMemoryError.

If an OutOfMemoryError occurs, the corresponding Stack trace is also printed.

Let’s examine the various OutofMemoryErrors.

java.lang.OutOfMemoryError: Java heap space

Java Heap space means that new objects cannot be allocated in the Java Heap.

The first solution to this problem is to see if the configured heap size is too small.

Of course, if you have a program that has been running for a long time, and suddenly this kind of problem occurs, you should be on alert. There could be a potential memory leak. Further analysis is required.

In another case, if Java objects implement finalize methods, then the objects will not be collected immediately when garbage collection comes. Instead, it is placed ina Finalization queue.

This queue is executed by the finalizer daemon thread. If the finalizer daemon thread executes more slowly than objects are placed in the finalizer queue, Java objects cannot be recycled in a timely manner.

If the application creates a high-priority thread, it is possible that the high-priority thread will cause objects to be placed on the Finalization queue more slowly than the finalizer daemon thread.

java.lang.OutOfMemoryError: GC Overhead limit exceeded

GC overhead Limit Exceeded indicates that the GC is always running, causing the Java program itself to execute very slowly.

If a Java program is doing GC 98% of the time, but only recovers 2% of the heap space, and that lasts five times. The Java. Lang. OutOfMemoryError will be thrown.

You can turn this function off with the following parameters.

-XX:-UseGCOverheadLimit
Copy the code

java.lang.OutOfMemoryError: Requested array size exceeds VM limit

This error means that the array to be allocated is larger than the heap size.

This problem occurs when, for example, the maximum heap size is set to 256M, but an array of 300M is allocated.

java.lang.OutOfMemoryError: Metaspace

Since JDK8, Metaspace has moved into Java’s local memory space. If the size of the Metaspace beyond the limit, then the Java. Lang. OutOfMemoryError will also be thrown.

The size of Metaspace can be set with 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 as 32 bits (object pointer compression).

Object pointer compression can be done by:

UseCompressedClassPointers
Copy the code

By default, this parameter is enabled.

We can use CompressedClassSpaceSize to set the size of the pointer compression space.

Note that only the Klass meta information is stored in the space set by CompressedClassSpaceSize, while the rest of the meta information is stored in Metaspace.

OutOfMemoryError: reason stack_trace_with_native_method

This error indicates that the local method has encountered an allocation failure.

This problem may require the operating system’s native debugging tools to resolve.

conclusion

This article introduces different types of OutofMemoryErrors, which I hope you found useful.

Author: Flydean program stuff

Link to this article: www.flydean.com/jvm-outofme…

Source: Flydean’s blog

Welcome to pay attention to my public number: procedures those things, more wonderful waiting for you!