java heap space, PermGen space 错误 使用jvisualvm监测设置合理值

使用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

时间: 2024-08-21 17:14:47

java heap space, PermGen space 错误 使用jvisualvm监测设置合理值的相关文章

windows系统bat方式启动tomcat出现java.lang.OutOfmemoryError:PermGen Space 错误

1.问题情景: 在部署项目时,将两个应用部署到同一个tomcat下,通过startup.bat启动服务时,控制台出现出现java.lang.OutOfmemoryError:PermGen Space 错误服务器环境:    系统版本:windows server 2008 r2    安装内存:64GB    处理器:8核(具体型号记不得了) 2.解决方法: PermGen Space:全称是Permanent Generation Space,是指内存的永久保存区域,这块内存主要是被JVM存

java.lang.OutOfMemoryError: PermGen space PermGen space & java.lang.OutOfMemoryError: Java heap space Heap siz

java.lang.OutOfMemoryError: PermGen space PermGen space 由-XX:PermSize  -XX:MaxPermSize 引起 java.lang.OutOfMemoryError: Java heap space Heap siz 由-Xms -Xmx 引起 Liunx下修改:catalina.sh # OS specific support.  $var _must_ be set to either true or false. JAVA

tomcat 开启时,或者访问网页后PermGen space错误解决方法 java.lang.OutOfMemoryError: PermGen space

公司以前都是一个项目一个tomcat,为了整合,我把两个项目放到了一个tomcat下,结果打开tomcat后,随便访问几个网页,或者登录一个账号后就会报错,提示 java.lang.OutOfMemoryError: PermGen space 上网搜索后,发现是JVM的默认内存只有4M太小,开启tomcat后要加载所有项目,就会导致JVM的内存溢出. Windows下tomcat修改 .编辑tomcat的catalina.bat文件,在第一行的后面增加一句:set JAVA_OPTS=-ser

Tomcat报java.lang.OutOfMemoryError: PermGen space错误

java.lang.OutOfMemoryError: PermGen space解决方案 : -Xms512m -Xmx1024m -XX:MaxPermSize=256m

myeclipse tomcat java.lang.OutOfMemoryError: PermGen space错误的解决方法

错误代码: java.lang.OutOfMemoryError: PermGen space 原因分析: myeclipse或tomcat的内容分配的不够用,启动失败 解决方法: 1.找到tomcat安装目录的这个文件\apache-tomcat-7.0.41\bin\catalina.bat,打开,在里面找到这句:rem ----- Execute The Requested Command .在这句下面添加:set JAVA_OPTS=%JAVA_OPTS% -Xms256m -Xmx10

异常处理系列:java.lang.OutOfMemoryError: PermGen space

今天帮同学解决他的Tomcat启动的问题,报出了一个OutOfMemoryError的异常,然后就搜啊搜的,突然发现特别有趣. 先不扯淡了,先说解决办法: 一看到这个就知道是内存满了.所以要把Tomcat的内存池加大一点. 修改如下: 1. window-->Preferences,然后直接在搜索的那个框里边写上"tomcat",会出现如下的界面: 2. 选中你安装的tomcat版本,选中下面的JDK,在右边的 Optional Java VM arguments 文本框中输入:

java.lang.OutOfMemoryError: PermGen space及其解决方法

PermGen space的全称是Permanent Generation space,是指内存的永久保存区域OutOfMemoryError: PermGen space从表面上看就是内存益出,解决方法也一定是加大内存.说说为什么会内存益出:这一部分用于存放Class和Meta的信息,Class在被 Load的时候被放入PermGen space区域,它和和存放Instance的Heap区域不同,GC(Garbage Collection)不会在主程序运行期对PermGen space进行清理

开发笔记--java.lang.OutOfMemoryError: PermGen space异常处理

第一次到公司开发项目,比起之前的小项目来说这次的项目特别大,以至于运行之后出现了java.lang.OutOfMemoryError: PermGen space的异常,从字面意思上来看是内存溢出的原因,这里引用别人的博客 1.PermGen space的全称是Permanent Generation space,是指内存的永久保存区域OutOfMemoryError: PermGen space从表面上看就是内存益出,解决方法也一定是加大内存.说说为什么会内存益出:这一部分用于存放Class和

java.lang.OutOfMemoryError: PermGen space从解决方法中认识堆与非堆

1)堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟机启动时创建的.”“在JVM中堆之外的内存称为非堆内存(Non-heap memory)”. 可以看出JVM主要管理两种类型的内存:堆和非堆.简单来说堆就是Java代码可及的内存,是留给开发人员使用的:非堆就是JVM留给自己用的, 所以方法区.JVM内部处理或优化所需的内存(如JIT编译后的代码缓存).每个类结构(如运行时常