Detailed explanation of Tomcat memory settings

Detailed explanation of Java memory overflow

 

1. There are three common Java memory overflows:

 

1.  java.lang.OutOfMemoryError: Java heap space  ---- JVM Heap (heap) overflow
JVM will automatically set the value of JVM Heap when it is started, and its initial space (ie -Xms) is 1/64 of the physical memory, The maximum space (-Xmx) cannot exceed physical memory.

It can be set by using options such as -Xmn -Xms -Xmx provided by the JVM. The size of the Heap is the sum of Young Generation and Tenured Generation.

In the JVM, if 98% of the time is used for GC, and the available Heap size is less than 2%, this exception message will be thrown.

Workaround: Manually set the size of the JVM 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 does the memory overflow? This is because this memory is mainly used by the JVM to store Class and Meta information. When the Class is loaded, it is put into the PermGen space area. It is different from the Heap area where the Instance is stored. Sun's GC will not be in the area. The PermGen space is cleaned up when the main program is running, so if your APP loads a lot of CLASS, it is likely to overflow the PermGen space.

Workaround: Manually set the MaxPermSize size

 

3.  java.lang.StackOverflowError    ---- The stack overflows
. The JVM still uses a stack-based virtual machine, which is the same as C and Pascal. The calling process of the function is reflected in the stack and pop-off stack.
There are so many "layers" of calling constructors that the stack overflows.
Generally speaking, the general stack area is much smaller than the heap area, because the function call process is often not more than a thousand layers, and even if each function call requires 1K space (this is approximately equivalent to declaring 256 in a C function). variable of type int), then the stack area only needs 1MB of space. Usually the stack size is 1-2MB.
Usually recursion should not have too many levels of recursion, it is easy to overflow.

Solution: Modify the program.

 

 

Second, the solution

 

In the production environment, if the tomcat memory setting is not good, it is easy to cause jvm memory overflow.

 

1.  Tomcat under linux:  

修改TOMCAT_HOME/bin/catalina.sh 
位置cygwin=false前。
JAVA_OPTS="-server -Xms256m -Xmx512m -XX:PermSize=64M -XX:MaxPermSize=128m" 

 

2.  If tomcat 5 is registered as a windows service and started in services mode, you need to modify the corresponding key value in the registry.

修改注册表HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Tomcat Service Manager\Tomcat5\Parameters\Java,右侧的Options
原值为
-Dcatalina.home="C:\ApacheGroup\Tomcat 5.0"
-Djava.endorsed.dirs="C:\ApacheGroup\Tomcat 5.0\common\endorsed"
-Xrs
加入 -Xms256m -Xmx512m 
重起tomcat服务,设置生效

 

3、如果tomcat 6 注册成了windows服务,或者windows2003下用tomcat的安装版,

在/bin/tomcat6w.exe里修改就可以了 。

 

 

 

 

 

4、 如果要在myeclipse中启动tomcat,上述的修改就不起作用了,可如下设置:

Myeclipse->preferences->myeclipse->servers->tomcat->tomcat×.×->JDK面板中的

Optional Java VM arguments中添加:-Xms256m -Xmx512m -XX:PermSize=64M -XX:MaxPermSize=128m

 

 

 

三、jvm参数说明:

 

-server:一定要作为第一个参数,在多个CPU时性能佳 
-Xms:java Heap初始大小。 默认是物理内存的1/64。
-Xmx:java heap最大值。建议均设为物理内存的一半。不可超过物理内存。

 


-XX:PermSize:设定内存的永久保存区初始大小,缺省值为64M。(我用visualvm.exe查看的)

-XX:MaxPermSize:设定内存的永久保存区最大 大小,缺省值为64M。(我用visualvm.exe查看的)

 

-XX:SurvivorRatio=2  :生还者池的大小,默认是2,如果垃圾回收变成了瓶颈,您可以尝试定制生成池设置

 

-XX:NewSize: 新生成的池的初始大小。 缺省值为2M。

-XX:MaxNewSize: 新生成的池的最大大小。   缺省值为32M。

如果 JVM 的堆大小大于 1GB,则应该使用值:-XX:newSize=640m -XX:MaxNewSize=640m -XX:SurvivorRatio=16,或者将堆的总大小的 50% 到 60% 分配给新生成的池。调大新对象区,减少Full GC次数。

 

 

 

 

 

+XX:AggressiveHeap 会使得 Xms没有意义。这个参数让jvm忽略Xmx参数,疯狂地吃完一个G物理内存,再吃尽一个G的swap。 
-Xss:每个线程的Stack大小,“-Xss 15120” 这使得JBoss每增加一个线程(thread)就会立即消耗15M内存,而最佳值应该是128K,默认值好像是512k. 

-verbose:gc 现实垃圾收集信息 
-Xloggc:gc.log 指定垃圾收集日志文件 
-Xmn:young generation的heap大小,一般设置为Xmx的3、4分之一 
-XX:+UseParNewGC :缩短minor收集的时间 
-XX:+UseConcMarkSweepGC :缩短major收集的时间 此选项在Heap Size 比较大而且Major收集时间较长的情况下使用更合适。

-XX:userParNewGC 可用来设置并行收集【多CPU】
-XX:ParallelGCThreads 可用来增加并行度【多CPU】
-XX:UseParallelGC 设置后可以使用并行清除收集器【多CPU】

 

原文出自:http://elf8848.iteye.com/blog/378805

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326481585&siteId=291194637