JAVA Memory Overflow Solution Summary __java

Source: Internet
Author: User


One reason

1. The amount of data loaded in memory is too large, such as to remove too much data from the database;

2. The collection class has a reference to the object, not emptied after use, so that the JVM can not be recycled;

3. Object entities in the code that have dead loops or loops that produce too many duplicates;

4. Bugs in third party software used;

5. The start parameter memory value set too small;


two common error tips

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


three principles of settlement

1. Application server prompts for error resolution: Set the boot parameter memory value is large enough.

2.Java code leads to error resolution: Focus on the following:

① checks to see if there is a dead loop or recursive call in the code.

② Check if there is a recurring cycle that duplicates the creation of new object entities.

③ Check to see if there is a query in the database query that gets all the data. In general, if you take 100,000 records to memory at a time, it can cause a memory overflow. This problem is more covert, before the line, the data in the database is less,

Not easy to problem, after the on-line, the database more data, a query may cause memory overflow. Therefore, the database query as far as possible in the way of paging query.

④ Check whether the list, map, and other collection objects have not been cleared after use. Collection objects such as List and map always have references to objects so that they cannot be reclaimed by GC.


Four Exception examples

1 tomcat Java.lang.OutOfMemoryError:Java heap space anomaly handling

①heap size

The JVM heap is set up to refer to the set of memory space that the JVM can use to deploy during the runtime of the Java program. The JVM automatically sets the value of heap size when it starts, and its initial space (that is,-XMS) is 1/64 of the physical memory, and the maximum space (-XMX) is 1/4 of the physical memory. You can use options such as the-XMN-XMS-XMX provided by the JVM to set it up. XMN represents the minimum value, XMS represents the initial value, and the XMX represents the maximum value. Heap size is the sum of young Generation and tenured generaion.

This exception information is thrown in the JVM if 98% of the time is for GC and the available heap size is less than 2%. Heap Size is not greater than 80% of the available physical memory, generally to set the-XMS and-XMX options to the same, and-xmn to the-XMX value of 1/4.


② workaround-Manually set heap size

Modify tomcat_home/bin/catalina.sh, add the following line to the "echo" Using catalina_base: $CATALINA _base "":

Java_opts= "-server-xms800m-xmx800m-xx:maxnewsize=256m"


2 Tomcat java.lang.OutOfMemoryError:PermGen space exception handling

①permgen Space

The full name of the PermGen space is permanent Generation, which refers to the permanent storage area of memory, which is mainly stored by the JVM class and meta information, Class is placed in the PermGen space when it is loader, unlike the heap region where the class instance (Instance) is stored, the GC (garbage Collection) does not clean up permgen spaces during the main program run. So if you have a lot of classes in your application, it's likely that permgen space errors will occur. This error is common when the Web server is pre compile the JSP. If your web app uses a large number of third-party jars that are larger than the JVM default size (4M), this error message is generated.


② workaround-Manually set MaxPermSize size

Modify tomcat_home/bin/catalina.sh, add the following line to the "echo" Using catalina_base: $CATALINA _base "":

Java_opts= "-server-xx:permsize=64m-xx:maxpermsize=128m

It is recommended that you move the same third-party jar files to the Tomcat/shared/lib directory to reduce the memory consumption of the jar document.

Java.lang.OutOfMemoryError exception Handling in 3 WebLogic

① error Prompts "Root cause of Ervletexception java.lang.OutOfMemoryError"

② solution to adjust 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

4 Eclipse Run JBoss java.lang.OutOfMemoryError:PermGen space exception handling

When running JBoss in Eclipse, the time is too long may sometimes appear java.lang.OutOfMemoryError:PermGen space error, here to introduce everyone a

Workaround.

① Click on the small arrow next to the debug icon

② Click "Debug configurations ..." menu item

③ Select JBoss v4.2 at localhost under the left "Generic Server" tree

④ Click on the "Arguments" 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

⑤ If you run JBoss by command line or by clicking "Run.bat" directly, you'll have to modify the JVM options in bin/run.conf file to find

Java_opts= "-xms128m-xmx512m ..." This paragraph, and then add "-xx:permsize=64m-xx:maxpermsize=256m" to the back.

⑥ Note: The numbers 128, 512, 64 and 256 can be adjusted according to the configuration of your machine, then click "Apply" to

Java.lang.OutOfMemoryError exception handling under 5 resin

① Reason

This error occurs, typically because the JVM has too little physical memory. The default Java virtual machine has a maximum memory of only 64 megabytes, which may not be a problem during development debugging, but is far from satisfying in a real-world application environment, unless your application is very small and there is little traffic. Otherwise you may find that the program runs for a period of time and reports Java.lang.OutOfMemoryError errors. So we need to increase the size of the virtual machine memory available to resin.


② Solution

Modifying the args option in/usr/local/resin/bin/httpd.sh, adding parameters-xms (initial memory) and-XMX (maximum memory size) can be used to limit the amount of physical memory used by the JVM. The JVM's initial physical memory, such as the args= "-xms128m-xmx256m" setting, is 128m, with the maximum physical memory being 256m. These two values should be set by the system administrator based on the actual situation of the server.


Five typical cases

1 Hibernate query data, one query too much data, and then adjust the code of that part, each time only take out the specified amount of data, successfully solve the problem.

2 in the stress test, the emergence of OutOfMemoryError, found that the session's resources have not been released from the

It is best to release the session's resources through the session's invalidate () method.

A dead loop occurs in the 3 program.

4 Tomcat deployment, run appear outofmemoryerror, increase memory parameter value, solve this problem.

Original post address: http://mars914.iteye.com/blog/1446497

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.