Three type of OutOfMemoryError in Java

Before solving the java memory overflow problem, you need have idea about the Java memory management. 

The JVM memory model have three different types of areas: Permanent Generation space , Heap space, Java Stacks (Java stack). 

The permanent storage area mainly stores Class and Meta information. When the Class is first loaded, it is placed in the PermGen space area. The content that the Class needs to store mainly includes methods and static properties. 

The heap area is used to store instances of Class (objects), and these objects are non-static properties. Every time an object instance is created with “new”, the object instance is stored in the heap area, and this space is also managed by the garbage collection of the jvm. 

The Java stack is similar to the stack functions of most programming languages including assembly language, the main basic types of variables and method input and output parameters. There is an independent stack in each thread of a Java program. So,Memory overflow problems include: Permanent Generation space and Heap space

1.OutOfMemoryError : PermGen space

This error root cause is a large number of jars or classes are used in the program, which makes the java virtual machine have not enough space to load classes, which is related to the Permanent Generation space. There are two ways to solve such problems:

    1)Increase the size of the XX:PermSize and XX:MaxPermSize parameters in the java virtual machine:

JAVA_OPTS=" -XX:PermSize=64M -XX:MaxPermSize=128m"

    2)Clean up the jars under web-inf/lib in the application. If multiple applications are deployed in tomcat, and many applications use the same jar, you can move the common jar to the common lib of tomcat to reduce repeated loading of classes

2.OutOfMemoryError: Java heap space

This problem occurs because the Java virtual machine creates too many objects.meanwhile , there objects can be reclaim by JVM.

To resolve this problem is a little bit complicate:

    1)Try to find out memory leak , this is a hard topic

    2)Set reference to null after used

    3)Increase memory:

JAVA_OPTS= -Xms256m -Xmx1024m

3.OutOfMemoryError:unable to create new native thread

This problem means that you create two many thread that exceed the system’s constraint.

    1)Redesign you thread model

    2)Increase the system thread constraint