Java. Lang. OutOfMemoryError is Java. Lang. VirtualMachineError subclass, when the JVM due to insufficient memory to allocate objects, and the garbage collector can’t provide more memory, throws this exception.

A virtual machine might construct an OutOfMemoryError as if the stack were unwritable or Suppression was forbidden.

Type of OutOfMemoryError:

There are two types of OutOfMemory:

The java.lang.OutOfMemoryError: Java heap space

The java.lang.OutOfMemoryError: PermGen space

Although both cases occur when the JVM runs out of memory, they are very different from each other, and their solutions are independent of each other.

Source code analysis:

  


As you can see, OutOfMemoryError has two constructors, one that takes no arguments and one that takes a verbose message.

Recreate the Java. Lang. OutOfMemoryError: Java heap space

The following code can reproduce OutOfMemory:

  


When running this program, the following problems occur:

  


In the example above, we can see that an OutOfMemoryError can be created by a small program because it is only a small error step. Because it has an infinite loop, constantly adding to the same variable.

Java. Lang. OutOfMemoryError: Java heap space solutions

The simple solution is to adjust the JVM parameters to increase the maximum heap size: set the -xmx / -xms ratio to 1:1 or 1:1.5 for example:

Export JVM_ARGS = “- Xms1024m – Xmx1024m”

If OutOfMemory occurs after resizing, we need to use the appropriate tools to investigate memory leaks and analyze the Heap Dump files of the program.

Common tools are:

Eclipse Memory Analyzer(MAT) analyzes Heap Dump

Profiler like Netbeans or JProbe.

Java. Lang. OutOfMemoryError: PermGen space

There are two main reasons for PermGen space problems:

We generally know that JVMS use generational garbage collection, with new generation, old generation, and persistent generation. PermGen is the persistent generation, so the persistent generation is used to store the JVM

Class, method, all kinds of String Pool, all kinds of metadata.

In most cases, the default size for the persistent generation is about 64MB, so in practice, this value is easy to achieve.

We need to adjust the value of this parameter:

“-xx: PermSize” and “-xx: MaxPermSize” are required.

Such as:

Export JVM_ARGS = “- Xmx1024m – XX: MaxPermSize = 256 m”

The second reason is: memory leakage caused by ClassLoader. Typically, this error occurs in GlassFish, Tomcat

Wait in the Web server.

Use different class loaders in the application server to load different applications so that one application can be deployed and unloaded without affecting other applications on the same server. But if the container somehow keeps a reference to any classes loaded by the application class loader during unload, then if the application is deployed and unloaded multiple times, that class and all related classes will not be garbage collected and quickly fill the permGen space.

Java. Lang. OutOfMemoryError: PermGen space solutions

As mentioned above, the first solution is to adjust the parameters,

Export JVM_ARGS = “- XX: PermSize = 64 m – XX: MaxPermSize = 256 m”

Of course, if the same problem will occur, you need to analyze the specific memory leakage situation.

Common tools are:

Eclipse Memory Analyzer(MAT) analyzes Heap Dump

Profiler like Netbeans or JProbe.

Above, memory leak analysis and solution, for reference.