Reason: The following are common:
1. The amount of data loaded in memory is too large, such as taking too much data from the database at once;
2. There are references to objects in the collection class, and they are not cleared after use, so that the JVM cannot be recycled;
3. There are dead loops in the code or loops that produce too many repetitions;
4. BUGs in third-party software used;
5. The memory value of the startup parameter is set too small;
Common error prompts:
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
solve:
1. Solution to the error prompted by the application server: Set the memory value of the startup parameter to be large enough.
2. Solutions to the error caused by Java code: Focus on the following points:
1) Check whether there are dead loops or recursive calls in the code.
2) Check whether there is a large loop that repeatedly produces new object entities.
3) Check whether there is a query that has obtained all the data in the database query. Generally speaking, if you take 100,000 records into memory at a time, it may cause memory overflow. This problem is quite hidden. Before going online, there was less data in the database and it was not easy to have problems. After going online, there was more data in the database, and a single query might cause memory overflow. Therefore, try to use pagination to query database queries.
4) Check whether the collection objects such as List, MAP have not been cleared after use. Collection objects such as List and MAP will always have references to the objects, making these objects unable to be recycled by GC.
Case:
1. When hibernate querying data, it querys too much data at one time. Later, the code in this part is adjusted, and only the specified amount of data is retrieved at a time, successfully solving the problem. 2. When doing stress tests, OutOfMemoryError appears and it is found that the resource of the session has not been released. It is best to release the resource of the session through the invalidate() method of the session.
3. A dead loop appears in the program.
4. OutOfMemoryError appears when the tomcat is deployed and run, and the memory parameter value is increased to solve this problem.
java.lang.OutOfMemoryError in tomcat: Java heap space exception handling
1. Heap size The settings of the JVM heap refer to the settings of the memory space that the JVM can provision during the Java program operation. When the JVM starts up, it will automatically set the value of Heap size. Its initial space (i.e. -Xms) is 1/64 of physical memory, and the maximum space (-Xmx) is 1/4 of physical memory. You can use the -Xmn -Xms -Xmx and other options provided by the JVM to set it. The size of Heap size is the sum of Young Generation and Tenured Generaion. Tip: This exception message will be thrown if 98% of the time is used for GC and the available Heap size is less than 2%. Tip: The maximum Heap Size should not exceed 80% of the available physical memory. Generally, set the -Xms and -Xmx options to the same, while -Xmn is 1/4 of the -Xmx value.
2. Solution: Manually set Heap size and modify TOMCAT_HOME/bin/catalina.sh Add the following line to "echo "Using CATALINA_BASE: $CATALINA_BASE"": JAVA_OPTS="-server -Xms800m -Xmx800m -XX:MaxNewSize=256m"
java.lang.OutOfMemoryError: PermGen space exception handling in tomcat
1. PermGen space The full name of PermGen space is Permanent Generation space, which refers to the permanent storage area of memory. This piece of memory is mainly stored by the JVM. Class will be placed in PermGen space when it is loaded. It is different from the Heap area where the class instance (Instance) is stored. GC (Garbage Collection) will not clean up PermGen space during the main program runtime. Therefore, if there are many CLASS in your application, it is very likely that PermGen space error will occur. This error is common when the web server precompiles JSP. If you use a large number of third-party jars under your WEB APP, the size exceeds the default size of jvm (4M), then this error message will be generated.
Solution: Manually set the MaxPermSize size and modify TOMCAT_HOME/bin/catalina.sh Add the following line to "echo "Using CATALINA_BASE: $CATALINA_BASE"": JAVA_OPTS="-server -XX:PermSize=64M -XX:MaxPermSize=128m Recommendation: Move the same third-party jar file to the tomcat/shared/lib directory, so as to achieve the purpose of reducing the repeated memory usage of jar documents.
java.lang.OutOfMemoryError exception handling in weblogic
Error prompt: "Root cause of ervletException java.lang.OutOfMemoryError"
Solution: Adjust the parameters in CommEnv in bea/weblogic/common: sun if "%PRODUCTION_MODE%" == "true" goto sun_prod_mode set JAVA_VM=-client set MEM_ARGS=-Xms256m -Xmx512m -XX:MaxPermSize=256m set JAVA_OPTIONS=%JAVA_OPTIONS% -Xverify:none goto continue :sun_prod_mode set JAVA_VM=-server set MEM_ARGS=-Xms256m -Xmx512m -XX:MaxPermSize=256m goto continue
java.lang.OutOfMemoryError when Eclipse runs Jboss: PermGen space exception handling
When running Jboss in Eclipse, if it takes too long, sometimes java.lang.OutOfMemoryError: PermGen space error. Here is a solution for you:
1) Click the small arrow next to the debug icon;
2) Click the "Debug Configurations..." menu item;
3) Select "JBoss v4.2 at localhost" under the "Generic Server" tree on the left;
4) Click the "Arguments" Tab on the right and add it 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 run JBoss in command line mode or directly click "run.bat", you have to modify the JVM options in the bin/run.conf file, find the JAVA_OPTS="-Xms128m -Xmx512m..." paragraph, and then add "-XX:PermSize=64m -XX:MaxPermSize=256m" to the end. Save it is OK.
6) Note: The numbers 128, 512, 64 and 256 can be adjusted according to the configuration of your machine, and then click "Apply".
java.lang.OutOfMemoryError exception handling under Resin
Reason: This error occurs because the JVM physical memory is too small. The default Java virtual machine has a maximum memory of only 64 megabytes, which may be no problem during development and debugging, but it is far from meeting the needs in actual application environments unless your application is very small and has no access. Otherwise you may find an error in the package java.lang.OutOfMemoryError after the program runs for a period of time. Therefore, we need to increase the size of the virtual machine memory available to the reservoir.
Solution: Modify the args option in /usr/local/resin/bin/httpd.sh to add parameters -Xms (initial memory) and -Xmx (maximum memory size can be used) which can be used to limit the physical memory usage of the JVM. For example: After setting args="-Xms128m -Xmx256m", the initial physical memory of JVM is 128m, and the maximum physical memory can be used is 256m. These two values should be set by the system administrator according to the actual situation of the server.
The above is the complete content of the perfect solution to the error handling problem of java.lang.OutOfMemoryError brought to you. I hope everyone will support Wulin.com~