Todd

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  65 随笔 :: 0 文章 :: 24 评论 :: 0 Trackbacks
使用myeclipse启动tomcat 报java heap space ,PermGen space 错误,分别为 heap内存不足,PermGen内存不足
需加大 tomcat启动项参数 Xmx 和 XX:MaxPermSize
PermGen是指内存的永久保存区域,它用于存放class和 method 对象,以及String 对象
(sun原文:permanent generation is the area of the heap where class and method objects are stored. If an application loads a very large number of classes, then the size of the permanent generation might need to be increased using the -XX:MaxPermSize option.
Interned java.lang.String objects are also stored in the permanent generation. The java.lang.String class maintains a pool of strings. When the intern method is invoked, the method checks the pool to see if an equal string is already in the pool. If there is, then the intern method returns it; otherwise it adds the string to the pool. In more precise terms, the java.lang.String.intern method is used to obtain the canonical representation of the string; the result is a reference to the same class instance that would be returned if that string appeared as a literal. If an application interns a huge number of strings, the permanent generation might need to be increased from its default setting.
When this kind of error occurs, the text String.intern or ClassLoader.defineClass might appear near the top of the stack trace that is printed.
The jmap -permgen command prints statistics for the objects in the permanent generation, including information about internalized String instances. See 2.6.4 Getting Information on the Permanent Generation.

PermGen又是一个特殊内存区域:Classloader 加载的东东是不能回收的,它们放在PermGen中
(tomcat原文:Why does the memory usage increase when I redeploy a web application? Because the Classloader (and the Class objects it loaded) cannot be recycled. They are stored in the permanent heap generation by the JVM, and when you redepoy a new class loader is created, which loads another copy of all these classes. This can cause OufOfMemoryErrors eventually.)

回到我的问题来,打开%java_home%bin\jvisualvm.exe  (jdk6以上有)
查看tomcat的内存情况,点击tomcat标签下monitor , 当used heap = max heap      used PermGen = max PermGen 时tomcat还在启动中,一会就报错;
由于此前已经将myeclipse中server->tomcat->tomcatx.x->jdk的参数设置成-Xms64m -Xmx512m -XX:MaxPermSize=80m
但是monitor 画面中max heap为 256M
点击overview标签,发现jvm参数如下:
-Xms64m
-Xmx512m
-XX:MaxPermSize=80m
-Xms64m
-Xmx256m

显然tomcat使用了最后的设置-Xms64m -Xmx256m;这个是在myeclipse的属性-》java->installed jres 中的vm启动参数里,双击默认启用的jre,把参数去掉或者设置成-Xms64m -Xmx512m
再回到server->tomcat->tomcatx.x->jdk中把-XX:MaxPermSize=80m修改为-XX:MaxPermSize=128m
保存后再启动tomcat,ok

另外,使用jvisualvm持续监测tomcat内存情况发现heap区域的内存使用会在一个高峰后稳定降低,内存被回收了;
而PermGen区域的内存则是不断增加到达一个峰值后就不再增加,但之后此区的内存没有被回收,验证了上面的说法;

多说两句:java的动态加载衍生出诸多框架,在空间上也暴露出问题,反射在时间上也存在效率问题:下面是组测试数据:

Java version 1.6.0_13
Java HotSpot(TM) Client VM
11.3-b02
Sun Microsystems Inc.

Direct access using member field:
 47 125 47 46 46
 average time = 66 ms.
Reference access to member field:
 109 109 110 94 109
 average time = 106 ms.
Reflection access to member field:
 13094 12984 13063 13062 13094
 average time = 13051 ms.

Java version 1.6.0_13
Java HotSpot(TM) Client VM
11.3-b02
Sun Microsystems Inc.

Direct call using member field:
 47 31 109 109 31
 average time = 70 ms.
Direct call using passed value:
 16 16 16 31 15
 average time = 20 ms.
Call to object using member field:
 46 47 47 47 32
 average time = 43 ms.
Call to object using passed value:
 15 16 31 16 16
 average time = 20 ms.
Reflection call using member field:
 812 782 844 844 844
 average time = 829 ms.
Reflection call using passed value:
 938 953 954 1031 953
 average time = 973 ms.

Java version 1.6.0_13
Java HotSpot(TM) Client VM
11.3-b02
Sun Microsystems Inc.

Direct Object creation:
 62 47 78 32 93
 average time = 63 ms.
Reflection Object creation:
 125 94 94 109 187
 average time = 121 ms.
Direct byte[8] creation:
 125 187 94 172 94
 average time = 137 ms.
Reflection byte[8] creation:
 266 171 187 188 219
 average time = 191 ms.
Direct byte[64] creation:
 250 172 156 125 203
 average time = 164 ms.
Reflection byte[64] creation:
 281 219 203 203 219
 average time = 211 ms.

华丽的上层需要坚实的底层基础

http://wiki.apache.org/tomcat/FAQ/Deployment
http://download.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/memleaks.html#gbyuu

posted on 2011-10-18 22:16 Todd 阅读(3639) 评论(1)  编辑  收藏 所属分类: eclipsejava

评论

# re: java heap space, PermGen space 错误 使用jvisualvm监测设置合理值 2011-10-20 08:34 tb
不错 又收藏一个错误  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: