"Reprint" Java project frequently encountered memory overflow problem: Java.lang.OutOfMemoryError:PermGen space, heap memory and non-heap memory, written very well, easy to understand

Source: Internet
Author: User
Tags xms

Tomcat Xms Xmx permsize maxpermsize Difference and java.lang.OutOfMemoryError:PermGen space solution

Solution added in front of the blue Code in Catalina.bat: Red code

Set java_opts=%java_opts%-server-xms800m-xmx1024m-xx:permsize=128m-xx:maxpermsize=256m echo Using catalina_base:    "%catalina_base%"    

Here is a description of the difference: Excerpt from: http://www.cnblogs.com/mingforyou/archive/2012/03/03/2378143.html

Eclipse crashes, error message:
MyEclipse has detected, less than 5% of the 64MB of Perm
Gen (non-heap memory) space remains. It is strongly recommended
That, you exit, and restart MyEclipse with the new virtual machine memory
Paramters to increase this memory. Failure to does so can result in
Data loss. The recommended Eclipse memory parameters is:
eclipse.exe-vmargs-xms128m-xmx512m-xx:permsize=64m-xx:maxpermsize=128m

1. Meaning of the parameter
-vmargs-xms128m-xmx512m-xx:permsize=64m-xx:maxpermsize=128m
The-vmargs description is followed by the parameters of the VM, so the following are actually the parameters of the JVM
-xms128m JVM Initial allocation of heap memory
-XMX512M JVM Maximum allowable allocated heap memory, on demand
-xx:permsize=64m JVM Initial allocation of non-heap memory
-xx:maxpermsize=128m JVM Maximum allowable allocated non-heap memory, on demand

Let's first look at the mechanism of JVM memory management and then explain what each parameter represents.


1) heap and non-heap (non-heap) memory


According to the official statement, "Java virtual machines have a heap, the heap is a runtime data region, and all class instances and arrays of memory are allocated from here." The heap is created when the Java virtual machine is started. "" The memory outside the heap in the JVM is called non-heap (non-heap memory) ".
You can see that the JVM primarily manages two types of memory: heap and non-heap. Simply put, the heap is the Java code of memory, is left to developers to use, non-heap is the JVM left to use,
Therefore, the code for the method area, internal processing or optimization of the JVM, such as the JIT-compiled code cache, and each class structure (such as running the constant pool, field, and method data), and the methods and construction methods are in non-heap memory.


Heap memory allocation


The initial allocated heap memory of the JVM is specified by-XMS, and the default is the maximum allocated heap memory for 1/64;JVM of physical memory specified by-XMX, which defaults to 1/4 of the physical memory. When the default free heap memory is less than 40%, the JVM will increase the heap until the maximum limit of-xmx;
When free heap memory is greater than 70%, the JVM will reduce the heap until the minimum limit of-XMS. So the server generally sets-xms,-xmx equal to avoid resizing the heap after each GC.
Note: If-XMX is not specified or is small, the application may cause a java.lang.OutOfMemory error, which is from the JVM, not throwable, and cannot be captured with Try...catch.


Non-heap memory allocation


The JVM uses-xx:permsize to set the non-heap memory initial value, which defaults to 1/64 of the physical memory, and the maximum non-heap memory by Xx:maxpermsize, which by default is 1/4 of physical memory. (There is also a saying: MaxPermSize default value is related to the-server-client option,
Under the-server option, the default maxpermsize for 64m,-client option under the default MaxPermSize is 32m. I don't have an experiment with this. )
The full name of the PermGen space in the above error message is permanent Generation space, which is the permanent storage area of the memory. It is not clear whether PermGen space is a non-heap memory, or is not a heap of memory, but at least it belongs to.
Xx:maxpermsize set too small will cause java.lang.OutOfMemoryError:PermGen space is memory benefits.
Tell me why memory benefits:
(1) This portion of memory is used to store class and meta information, and class is placed in the PermGen space area when it is loaded, unlike the heap area where the instance is stored.
(2) GC (garbage Collection) does not clean up permgen space during the main program run time, so if your app will load many classes, PermGen space error is likely to occur.
This error is common when the Web server pre-compile the JSP.


2) JVM memory limit (max)


First, the JVM memory is limited to the actual maximum physical memory, assuming that the physical memory is infinitely large, the maximum value of the JVM memory is very much related to the operating system. In short, the 32-bit processor, although the controllable memory space has 4GB, but the specific operating system will give a limit,
This limit is generally 2GB-3GB (typically under Windows systems for the 1.5g-2g,linux system 2g-3g), and processors over 64bit are not limited.

2. Why do some machines I set-xmx and-xx:maxpermsize to 512M after eclipse can be started and some machines will not boot?
  Through the above introduction to JVM memory management we have learned that JVM memory consists of two kinds: heap memory and non-heap memory, and the JVM's maximum memory depends first on actual physical memory and operating system. So there are several reasons why setting a VM parameter causes the program not to start: The value of-XMS in the
1) parameter is greater than-XMX, or the value of-xx:permsize is greater than-xx:maxpermsize;
2)-xmx value and-xx: The sum of the maxpermsize exceeds the maximum JVM memory limit, such as the current operating system maximum memory limit, or actual physical memory, and so on. Speaking of actual physical memory here is a point,
  If your memory is 1024MB, but the actual system used is not likely to be 1024MB, because a part of the hardware is occupied.

3. Why is the above parameter written to the Eclipse.ini file eclipse does not perform the corresponding settings?
Why is the same argument valid on a shortcut or command line and is not valid in the Eclipse.ini file? This is because we have not complied with the setup rules for Eclipse.ini files:
Parameter shape such as "Item Value" In this form, there are spaces in the middle of the need to write a newline, if there are spaces in the value need to be enclosed in double quotation marks. For example, we use the-VM c:/java/jre1.6.0/bin/javaw.exe parameter to set the virtual machine,
In the Eclipse.ini file, write this:
-vm
C:/java/jre1.6.0/bin/javaw.exe
-vmargs
-xms128m
-xmx512m
-xx:permsize=64m
-xx:maxpermsize=128m
The results of the actual operation can be viewed through the "Configuration Details" button in the "Help" – "About Eclipse SDK" window in Eclipse.
It is also necessary to note that the contents of the Eclipse.ini file included in the Eclipse compression package are as follows:
-showsplash
Org.eclipse.platform
--launcher. Xxmaxpermsize
256m
-vmargs
-xms40m
-xmx256m
Which –launcher. Xxmaxpermsize (Note that the front is two connections) the meaning of the-xx:maxpermsize parameter is basically the same, I think the only difference is that the former is the Eclipse.exe set parameters when starting,
The latter is the parameter in the JVM used by Eclipse. In fact, the two set a can, so here can put –launcher. Xxmaxpermsize and Next line use # comment out.

4. Other start-up parameters. If you have a dual-core CPU, you might be able to try this parameter:
-xx:+useparallelgc
Allows the GC to execute faster. (only new parameters added to the GC in JDK 5)

Add:
If you have a large number of third-party jars under your web app that are larger than the default size of the server JVM, there will be a memory benefit problem.
Workaround: Set the MaxPermSize size
You can add server-initiated JVM parameter settings by selecting the appropriate server, such as TOMCAT5, in Myelipse to expand the JDK subkey page on the inside:
-xms128m
-xmx256m
-xx:permsize=128m
-xx:maxnewsize=256m
-xx:maxpermsize=256m
or manually set the MaxPermSize size, such as Tomcat,
To modify the Tomcat_home/bin/catalina.bat, add the following line above the echo "Using catalina_base: $CATALINA _base":
Java_opts= "-server-xx:permsize=64m-xx:maxpermsize=128m

Recommendation: Move the same third-party jar files to the Tomcat/shared/lib directory, which reduces the memory consumption of the jar document

"Reprint" Java project frequently encountered memory overflow problem: Java.lang.OutOfMemoryError:PermGen space, heap memory and non-heap memory, written very well, easy to understand

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.