Java Memory Overflow Detailed

Source: Internet
Author: User


One, the common Java memory overflow has the following three kinds:

1.java.lang.outofmemoryerror:java Heap Space----JVM heap(heap) overflow

The JVM automatically sets the value of jvmheap when it is started, and its initial space ( that is, -xms) is the 1/64of physical memory, maximum space (-XMX) do not exceed physical memory.

You can use options such as the -xmn-xms-xmx provided by the JVM to set it up. The size of the Heap is the sum of younggeneration and tenuredgeneraion .

This exception information is thrown in the JVM if 98% of time is used for GCand the available heapsize is less than 2.

Workaround: Manually set the size of the jvmheap(heap).

2.java.lang.outofmemoryerror:permgen space----permgen space overflow.

The full name of the permgenspace is permanentgeneration space, which refers to the memory's permanent storage area.

Why memory overflow, this is because this block of memory is mainly JVM Storage class and Meta information, class in load is placed in permgenspace area, it and storage instance Heap zone differs ,sun GC does not run during the main program permgenspace for cleanup, so if your app will load many class , it is likely to appear permgenspace overflow.

Workaround: Manually set the maxpermsize size

3.java.lang.stackoverflowerror---- Stack Overflow

Stack overflows, theJVM is still a stack-based virtual machine, which is the same as C and Pascal . The procedure for calling a function is on the stack and on the fallback.

There are too many "layers" of calls to the constructor to overflow the stack area.

Generally speaking, the general stack area is much smaller than the heap area, because the function call process often does not have more than thousands of layers, and even if each function call requires 1K of Space ( this is about the equivalent of a C function declared in a variable of type int ), then the stack area is just a space that requires 1MB . The size of the stack is usually 1 .

2MB .

Often recursion does not have too many levels of recursion, it is easy to overflow.

Workaround: Modify the program.

Second, the solution

In a production environment, Tomcat memory settings are not good enough to be prone to JVM memory overflow.

1. TomcatunderLinux :

Modify tomcat_home/bin/catalina.sh

Add the following line to the "echo" Using catalina_base: $CATALINA _base "" :

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

2. If Tomcat 5 is registered as a Windows service and is started by services, you need to modify the corresponding key values in the registry.

Modify the registry Hkey_local_machine\software\apachesoftware Foundation\tomcat Servicemanager\tomcat5\parameters\java , Options on the right

The original value is

-dcatalina.home= "C:\ApacheGroup\Tomcat5.0"

-djava.endorsed.dirs= "C:\ApacheGroup\Tomcat5.0\common\endorsed"

-xrs

Join -xms256m-xmx512m

Restart Tomcat service , set to take effect

3. If tomcat6 is registered as a Windows service, or windows2003 under the installation version of Tomcat ,

It can be changed in /bin/tomcat6w.exe .


4, if you want to start tomcatin MyEclipse , the above modification will not work, can be set as follows:

In the myeclipse->preferences->myeclipse->servers->tomcat->tomcatx.x->jdk panel

Optionaljava VM Arguments added:-xms256m-xmx512m-xx:permsize=64m-xx:maxpermsize=128m

Third,JVM parameter Description:

-server: must be the first parameter, good performance on multiple CPUs

-xms:javaheap Initial size. The default is the 1/64of physical memory.

-xmx:javaheap maximum value. The recommendations are set to half of physical memory. Do not exceed physical memory.


-xx:permsize: Sets the initial size of the memory's permanent save, the default value is 64M. (I use visualvm.exe to view)

-xx:maxpermsize: Sets the maximum size of the memory's permanent save, the default value is 64M. (I use visualvm.exe to view)

-xx:survivorratio=2: The size of the survivor pool, default is 2, if garbage collection becomes a bottleneck, you can try to customize the build pool settings

-xx:newsize: The initial size of the newly generated pool. The default value is 2M.

-xx:maxnewsize: The maximum size of the newly generated pool. The default value is 32M.

If the JVM 's heap size is larger than 1GB, the value should be used:-xx:newsize=640m-xx:maxnewsize=640m-xx:survivorratio=16, or assign the total size of the heap 50% to 60% to the newly generated pool. Increase the number of FULLGC in the area of the object.

+xx:aggressiveheap will make Xms meaningless. This parameter allows the JVM to ignore the Xmx parameter , frantically eating a g of physical memory , and then eating a g swap.

-xss: Stack size per thread, "-xss15120" This causes JBoss to consume 15M per additional thread. memory, and the best value should be 128K, and the default value seems to be 512k.


-VERBOSE:GC Reality garbage collection Information

-xloggc:gc.log specifying garbage collection log files

-xmn:younggeneration heap size, generally set to Xmx of 3,4 of one

-XX:+USEPARNEWGC: Shortening the time of minor collection

-XX:+USECONCMARKSWEEPGC: Shorten major collection time this option is larger in the Heap size and major It is more appropriate to use a longer collection time.

-XX:USERPARNEWGC can be used to set up parallel collection "multi- CPU"

-xx:parallelgcthreads can be used to increase the degree of parallelism "multi- CPU"

-XX:USEPARALLELGC can be set to use parallel purge collector "multi- CPU"

Java Memory Overflow Detailed

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.