---
title: "PermGen space错误解决方法"
author: "wt_kelly"
date: "2014-12-19T10:52:03Z"
tags: []
slug: "permgen-space"
draft: false
lastmod: "2014-12-19T10:52:03Z"
id: 2168570
url: "https://wt-kelly.iteye.com/blog/2168570"
---

## PS：2014-1-23

在看下文之前，首先要确认意见事情，就是你是如何启动tomcat的，我们在平时的开发环境当中，都是通过startup.bat方式启动tomcat的，那么你按照下面的方式，去修改/bin/catalina.bat是没有问题的。但是如果你是生产环境下，我们一般都希望使用windows服务方式去启动tomcat，此时之前修改的配置文件是没有用的。因为windows服务启动tomcat不再去加载catalina.bat当中的参数了，而是去加载注册表中的参数，所以我们需要修改注册表。

HKEY\_LOCAL\_MACHINE/SOFTWARE/Apache Software Foundation/Procrun 2.0/Tomcat\_APPNAME/Parameters/Java，修改JvmMs和JvmMx的值，当前我都将其设定为1024，也就是1个G的容量。具体性能再后面继续观察。 之前还真不知道windows服务启动和startup.bat启动的区别。 windows服务器启动是在注册表中加载参数，startup.bat启动是在catalina.bat加载参数。

![](http://images.cnitblog.com/blog/80896/201401/232321393198.png)

## 1.参考：

[http://blog.csdn.net/fox009/article/details/5633007](http://blog.csdn.net/fox009/article/details/5633007)

[http://hi.baidu.com/like\_dark/blog/item/19c1948b3292b0799f2fb468.html](http://hi.baidu.com/like_dark/blog/item/19c1948b3292b0799f2fb468.html)

[http://anyeeye.iteye.com/blog/444624](http://anyeeye.iteye.com/blog/444624)

[Tomcat6性能调优 出现java.lang.OutOfMemoryError: PermGen space](http://hi.baidu.com/chssheng2007/blog/item/22e7b1d37d7f480b3af3cf18.html)

[http://www.mkyong.com/tomcat/tomcat-javalangoutofmemoryerror-permgen-space/](http://www.mkyong.com/tomcat/tomcat-javalangoutofmemoryerror-permgen-space/)

## 2.报错：

```
Exception in thread "DispatcherThread" java.lang.OutOfMemoryError: PermGen space
Exception in thread "ContainerBackgroundProcessor[StandardEngine[Catalina]]" java.lang.OutOfMemoryError: PermGen space
Exception in thread "State Saver" java.lang.OutOfMemoryError: PermGen space
Exception in thread "AWT-Windows" java.lang.OutOfMemoryError: OutOfMemoryError
```

## 3.原因：

PermGen space的全称是Permanent Generation space,是指内存的永久保存区域,这块内存主要是被JVM存放Class和Meta信息的,Class在被Loader时就会被放到PermGen space中,它和存放类实例(Instance)的Heap区域不同,GC(Garbage Collection)不会在主程序运行期对PermGen space进行清理，所以如果你的应用中有很多CLASS的话,就很可能出现PermGen space错误,这种错误常见在web服务器对JSP进行pre compile的时候。如果你的WEB APP下都用了大量的第三方jar, 其大小超过了jvm默认的大小(4M)那么就会产生此错误信息了。

## 4.解决方法1：

手动设置MaxPermSize大小，如果是linux系统，修改TOMCAT\_HOME/bin/catalina.sh，如果是windows系统，修改TOMCAT\_HOME/bin/catalina.bat，  
在“echo "Using CATALINA\_BASE: $CATALINA\_BASE"”上面加入以下行：  
JAVA\_OPTS="-server -XX:PermSize=64M -XX:MaxPermSize=128m  
**建议：将相同的第三方jar文件移置到tomcat/shared/lib目录下，这样可以达到减少jar 文档重复占用内存的目的。**

## 5.解决方法2

修改eclipse.ini文件，修改如下：

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

```
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms128m
-Xmx512m
-XX:PermSize=64M 
-XX:MaxPermSize=128M
```

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

如果还报错，可以考虑如下修改

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

```
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms512m
-Xmx1024m
-XX:PermSize=256M 
-XX:MaxPermSize=512M
```

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

报错：

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

```
2011-11-21 21:10:46 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
严重: The web application [/Application] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
2011-11-21 21:10:46 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
严重: The web application [/Application] appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak.
2011-11-21 21:10:46 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
严重: The web application [/Application] appears to have started a thread named [AWT-Windows] but has failed to stop it. This is very likely to create a memory leak.
2011-11-21 21:10:46 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
严重: The web application [/Application] appears to have started a thread named [Thread-14] but has failed to stop it. This is very likely to create a memory leak.
2011-11-21 21:10:46 org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap
严重: The web application [/Application] created a ThreadLocal with key of type [net.sf.json.AbstractJSON$1] (value [net.sf.json.AbstractJSON$1@3661eeb]) and a value of type [java.util.HashSet] (value [[]]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak.
2011-11-21 21:10:50 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'Dispatcher'
```

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

修改catalina.bat

添加

```
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 
-server -Xms1536m -Xmx1536m
-XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m 
-XX:MaxPermSize=256m -XX:+DisableExplicitGC"
```

样例

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

```
#   JSSE_HOME       (Optional) May point at your Java Secure Sockets Extension
#                   (JSSE) installation, whose JAR files will be added to the
#                   system class path used to start Tomcat.
#
#   CATALINA_PID    (Optional) Path of the file which should contains the pid
#                   of catalina startup java process, when start (fork) is used
#
# $Id: catalina.sh 609438 2008-01-06 22:14:28Z markt $
# -----------------------------------------------------------------------------
 
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1536m 
-Xmx1536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m 
-XX:MaxPermSize=256m -XX:+DisableExplicitGC"
 
 
# OS specific support.  $var _must_ be set to either true or false.
cygwin=false
os400=false
darwin=false
case "`uname`" in
CYGWIN*) cygwin=true;;
OS400*) os400=true;;
Darwin*) darwin=true;;
esac
 
# resolve links - $0 may be a softlink
PRG="$0"
```

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

具体参数根据自己机器情况而定

```
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms512m 
-Xmx512m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m 
-XX:MaxPermSize=256m -XX:+DisableExplicitGC"
```