Java Memory Overflow Detailed

Source: Internet
Author: User

Java Memory Overflow detailed

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

1. Java.lang.OutOfMemoryError:Java heap Space----JVM heap (heap) overflow
The JVM will automatically set the JVM heap value when it is started, and its initial space (that is,-XMS) is 1/64 of the physical memory, and the maximum space (-XMX) cannot exceed the 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 young Generation and tenured generaion.
This exception information is thrown in the JVM if 98% of the time is used for GC, and the available heap size is less than 2%.
Workaround: Manually set the size of the JVM heap (heap).

2. Java.lang.OutOfMemoryError:PermGen space----permgen space overflow.
the full name of PermGen space is permanent Generation space, which refers to the permanent storage area of memory.
Why memory overflow, this is because the memory is mainly stored by the JVM class and meta information, class in the load is loaded into the PermGen space area, which is stored in the instance heap area, sun The GC does not clean up permgen space during the main program's run time, so if your app loads many classes, there's a good chance that permgen space will overflow.
Workaround: Manually set the MaxPermSize size

3. Java.lang.StackOverflowError----Stack Overflow
stack overflows, the JVM 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 is often not more than thousands of layers, and even if each function call requires 1K of space (this is approximately equivalent to a C function declared 256 int type variable), then the stack area is just need 1MB of space. 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. Tomcat under Linux:
Modify Tomcat_home/bin/catalina.sh
position Cygwin=false before.
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\apache software Foundation\tomcat Service Manager\tomcat5\parameters\java, Options on the right
The original value is
-dcatalina.home= "C:\ApacheGroup\Tomcat 5.0"
-djava.endorsed.dirs= "C:\ApacheGroup\Tomcat 5.0\common\endorsed"
-xrs
Join-xms256m-xmx512m
Restart Tomcat Service, set to take effect

3. If Tomcat 6 is registered as a Windows service, or WINDOWS2003 is installed under Tomcat,
it can be changed in/bin/tomcat6w.exe.

If it is an uncompressed version of Tomcat, you will need to find the corresponding decompression installation path:

For example, the path to my Tomcat decompression is:

\tomcat\tomcat_6\apache-tomcat-6.0.44\bin

Below this path, locate Catalina.bat (if it is windows, catalina.sh), open the file using the Notepad compiler

At the beginning of the document join:

Set java_opts=-xms1024m-xmx1024m-xx:permsize=256m-xx:maxnewsize=256m-xx:maxpermsize=256m

4. Idea how to set up Tomcat's boot memory

You can join in the red circle again:

-xms1024m-xmx1024m-xx:permsize=256m-xx:maxnewsize=256m-xx:maxpermsize=256m

Restart Tomcat ~

Also for idea, if you want to modify the JVM's memory, you can refer to the http://www.oschina.net/question/1040722_140162






4, if you want to start Tomcat in MyEclipse, the above modifications will not work, you can set the following:
Span style= "FONT-SIZE:16PX;" The
MYECLIPSE->PREFERENCES->MYECLIPSE->SERVERS->TOMCAT->TOMCATX.X->JDK panel = "FONT-SIZE:16PX;" >optional Java VM arguments added:-xms256m-xmx512m-xx:permsize=64m-xx:maxpermsize=128m





-server: Must be the first parameter, good performance on multiple CPUs
-xms:java Heap Initial size. The default is 1/64 of physical memory.
-xmx:java heap. 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.
-xx:maxpermsize: Sets the maximum size of the memory's Visualvm.exe, with a default value of 64M.

-xx:survivorratio=2: Survivor Pool Size, default is 2, if garbage collection becomes a bottleneck, visualvm.exe. You can try customizing 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.






+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, "-XSS 15120" This causes JBoss to consume 15M of memory as soon as each additional thread is added (thread). The best value should be 128K, and the default value would appear to be 512k.

-VERBOSE:GC Reality Garbage Collection Information
-xloggc:gc.log specifying garbage collection log files
The heap size of the-xmn:young generation, typically set to one of the 3, 4 points of Xmx
-XX:+USEPARNEWGC: Shortening the time of minor collection
-XX:+USECONCMARKSWEEPGC: Shorten major collection time this option is more appropriate if the heap size is large and major collection time is longer.
-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"

Original address: http://elf8848.iteye.com/blog/378805

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.