Perfectly solves the problem of java.lang.OutOfMemoryError handling errors

  • 2020-05-27 05:32:12
  • OfStack

Reasons: the following are common:

1. The amount of data loaded in memory is too large, for example, too much data is extracted from the database at one time;

2. There is a reference to the object in the collection class, which is not emptied after use, so JVM cannot be recycled;

3. There are dead loops in the code or object entities with too many repetitions generated by loops;

4. BUG in the third party software used;

5. The memory value of startup parameter is set too small;

Common errors:

1.tomcat:java.lang.OutOfMemoryError: PermGen space

2.tomcat:java.lang.OutOfMemoryError: Java heap space

3.weblogic:Root cause of ServletException java.lang.OutOfMemoryError

4.resin:java.lang.OutOfMemoryError

5.java:java.lang.OutOfMemoryError

Solution:

1. Application server prompts for error resolution: set the boot parameter memory value sufficiently large.

2. Resolution of errors caused by Java code:

1) check your code for dead loops or recursive calls.

2) check whether there is a large loop that generates new object entities repeatedly.

3) check whether there is a query to get all the data once in the database query. 1 in general, if 100, 000 records are taken to memory at a time, memory overflow may occur. This problem is relatively hidden. Before going online, there is less data in the database, so it is not easy to have a problem. After going online, there is more data in the database, and one query may cause memory overflow. Therefore, database queries should be paginated as much as possible.

4) check whether List, MAP and other collection objects are not cleared after use. Collection objects such as List, MAP, and so on will always have references to the objects so that they cannot be recycled by GC.

Case study:

1. When hibernate queried data, it queried too much data at one time. Later, it adjusted the code of this part and only took out a specified amount of data at a time. 2. When doing the stress test, OutOfMemoryError appears and it is found that the resource 1 of session has not been released. It is better to release the resource of session through invalidate() method of session.

3. There is a dead loop in the program.

4. OutOfMemoryError occurs in the deployment and operation of tomcat. Increase the value of memory parameters to solve this problem.

tomcat java.lang.OutOfMemoryError: Java heap space exception handling

The setting of Heap size JVM heap refers to the setting of the memory space that JVM can allocate during the operation of java program.JVM will automatically set the value of Heap size at startup, with the initial space (i.e. -Xms) being 1/64 of the physical memory and the maximum space (-Xmx) being 1/4 of the physical memory. You can set this using options such as -Xmn-Xms-Xmx provided by JVM. The size of Heap size is the sum of Young Generation and Tenured Generaion. Tip: in JVM this exception message is thrown if 98% of the time is spent on GC and less than 2% of Heap size is available. Tip: Heap Size should not exceed 80% of the available physical memory at most. For 1, set the -Xms and -Xmx options to the same, while -Xmn is 1/4 of the -Xmx value.

2. Solution: manually Heap size modify TOMCAT_HOME/bin/catalina sh in "echo" Using CATALINA_BASE: $CATALINA_BASE "" above to join the following line: JAVA_OPTS = "- server - Xms800m - Xmx800m - XX: 256 m MaxNewSize ="

tomcat java. lang. OutOfMemoryError: PermGen space exception handling

1. The full name of PermGen space PermGen space is Permanent Generation space, which refers to the permanent storage area of memory. This memory is mainly used by JVM to store Class and Meta information, and Class will be put into PermGen space when it is Loader. GC(Garbage Collection) does not clean up PermGen space during the main program run time, so if you have a lot of CLASS in your application, you are more likely to have an PermGen space error, which is common when web servers are doing pre compile. This error message is generated if your WEB APP USES a large number of third party jar and its size exceeds the default size for jvm (4M).

Solution: manually modify TOMCAT_HOME MaxPermSize size/bin/catalina sh in "echo" Using CATALINA_BASE: $CATALINA_BASE "" above to join the following line: JAVA_OPTS=" -server-XX =" -server-PermSize = 64M-PermSize :MaxPermSize=128m suggestion: move the same third party jar file to the tomcat/shared/lib directory, so as to reduce the repeated memory consumption of jar documents.

weblogic java.lang.OutOfMemoryError exception handling

Error: "Root cause of ervletException java. lang. OutOfMemoryError"

Solutions: Adjust CommEnv parameters in bea/weblogic/common :sun if "%PRODUCTION_MODE%" == "true" sun_prod_set set JAVA_VM= -client set MEM_ARGS= -Xms256m = -m512m =%JAVA_OPTIONS JAVA_VM= -server set MEM_ARGS= -Xms256m-Xmx512m-XX :MaxPermSize=256m goto continue

Eclipse: PermGen space exception handling when Jboss is running java.lang.OutOfMemoryError: PermGen space exception handling

java.lang.OutOfMemoryError: PermGen space when running Jboss in Eclipse, the time is too long and the error may sometimes occur. Here is a solution:

1) click the small arrow next to the debug icon;

2) click "Debug Configurations..." Menu item;

3) select "JBoss v4.2 at localhost" under the "Generic Server" tree on the left;

4) click the "Arguments" Tab TAB on the right and add in "VM arguments" :

-Dprogram.name=run.bat -Djava.endorsed.dirs="D:/JBoss405/bin/../lib/endorsed" -Xms128m -Xmx512m -XX:PermSize=64m -XX:MaxPermSize=256m

5) if you are running JBoss in command line mode or by clicking "run.bat", you will need to modify the JVM option in bin/ run.conf file. Find JAVA_OPTS=" -Xms128m512m..." This 1 paragraph, and then after that add "-XX :PermSize= 64m-XX :MaxPermSize=256m". Save it for OK.

6) note: the Numbers 128, 512, 64 and 256 can be adjusted according to the configuration of the machine, and then click "Apply".

Resin java. lang. OutOfMemoryError exception handling

Reason: this error occurs because JVM physical memory is too small. The default Java virtual machine has a maximum memory of only 64 megabytes, which may be fine during development and debugging, but is far from sufficient in a real-world application environment, unless your application is very small and has little traffic. Otherwise you may find errors in the package java.lang.OutOfMemoryError after the program has been running for 1 period. So we need to increase the size of virtual machine memory available for resin.

Solution: modify/usr/local/resin/bin/httpd sh args options in the add parameters - Xms (initial memory) and - Xmx (maximum can use memory size) can be used to restrict JVM physical memory usage. For example, after setting args=" -Xms128m-Xmx256m ", the initial physical memory of JVM is 128m, and the maximum physical memory available is 256m. These two values should be set by the system administrator based on the actual situation of the server.


Related articles: