简体   繁体   English

如何清除tomcat中的永久代空间错误

[英]How to clear PermGen space Error in tomcat

I am working in a Windows Environment, and I am getting this error every time I am working with tomcat-我在 Windows 环境中工作,每次使用 tomcat 时都会收到此错误-

Apr 30, 2012 5:30:37 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet default threw exception
java.lang.OutOfMemoryError: PermGen space
2012-04-30 17:30:37.719 INFO net.spy.memcached.MemcachedConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@4ae53a99
2012-04-30 17:30:37.719 INFO net.spy.memcached.MemcachedConnection:  Reconnecting due to failure to connect to {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0}
java.net.ConnectException: Connection refused: no further information
Apr 30, 2012 5:30:37 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet default threw exception
java.lang.OutOfMemoryError: PermGen space
Exception in thread "Memcached IO over {MemcachedConnection to localhost/127.0.0.1:11211}" java.lang.OutOfMemoryError: PermGen space
Apr 30, 2012 5:30:38 PM org.apache.coyote.http11.Http11Processor process
SEVERE: Error processing request
java.lang.OutOfMemoryError: PermGen space
Apr 30, 2012 5:30:38 PM org.apache.coyote.http11.Http11Processor process
SEVERE: Error processing request
java.lang.OutOfMemoryError: PermGen space
Apr 30, 2012 5:30:38 PM org.apache.coyote.http11.Http11Processor process
SEVERE: Error processing request
java.lang.OutOfMemoryError: PermGen space
Apr 30, 2012 5:30:41 PM org.apache.catalina.connector.CoyoteAdapter service
SEVERE: An exception or error occurred in the container during the request processing
java.lang.OutOfMemoryError: PermGen space
Apr 30, 2012 5:30:43 PM org.apache.catalina.core.StandardHostValve custom
SEVERE: Exception Processing ErrorPage[exceptionType=java.lang.Throwable, location=/error.action]
java.lang.OutOfMemoryError: PermGen space
Apr 30, 2012 5:30:42 PM org.apache.catalina.core.StandardHostValve custom
SEVERE: Exception Processing ErrorPage[exceptionType=java.lang.Throwable, location=/error.action]
java.lang.OutOfMemoryError: PermGen space
Exception in thread "ContainerBackgroundProcessor[StandardEngine[Catalina]]" java.lang.OutOfMemoryError: PermGen space

I tried adding JAVA_OPTS with the value as -Xms1024m -Xmx1024m in System Variables, but I am stillgetting the same error (java.lang.OutOfMemoryError: PermGen space) again and again.我尝试在系统变量中添加JAVA_OPTS with the value as -Xms1024m -Xmx1024m ,但我JAVA_OPTS with the value as -Xms1024m -Xmx1024m相同的错误(java.lang.OutOfMemoryError: PermGen space)

Any help will be appreciated.任何帮助将不胜感激。 I have also read other posts on Stack Overflow also but it didn't work out.我还阅读了 Stack Overflow 上的其他帖子,但没有成功。

The PermGen space is what Tomcat uses to store class definitions (definitions only, no instantiations) and string pools that have been interned. PermGen 空间是 Tomcat 用来存储类定义(仅定义,没有实例化)和已被实习的字符串池的空间。 From experience, the PermGen space issues tend to happen frequently in dev environments really since Tomcat has to load new classes every time it deploys a WAR or does a jspc (when you edit a jsp file).根据经验,PermGen 空间问题往往在开发环境中经常发生,因为 Tomcat 每次部署 WAR 或执行 jspc(当您编辑 jsp 文件时)都必须加载新类。 Personally, I tend to deploy and redeploy wars a lot when I'm in dev testing so I know I'm bound to run out sooner or later (primarily because Java's GC cycles are still kinda crap so if you redeploy your wars quickly and frequently enough, the space fills up faster than they can manage).就我个人而言,我在进行开发测试时倾向于经常部署和重新部署战争,所以我知道我迟早会用完(主要是因为 Java 的 GC 周期仍然有点废话,所以如果你快速和频繁地重新部署战争足够了,空间填满的速度比他们管理的要快)。

This should theoretically be less of an issue in production environments since you (hopefully) don't change the codebase on a 10 minute basis.从理论上讲,这在生产环境中应该不是问题,因为您(希望)不要在 10 分钟内更改代码库。 If it still occurs, that just means your codebase (and corresponding library dependencies) are too large for the default memory allocation and you'll just need to mess around with stack and heap allocation.如果它仍然发生,那只是意味着您的代码库(和相应的库依赖项)对于默认内存分配来说太大了,您只需要处理堆栈和堆分配。 I think the standards are stuff like:我认为标准是这样的:

-XX:MaxPermSize=SIZE

I've found however the best way to take care of that for good is to allow classes to be unloaded so your PermGen never runs out:然而,我发现解决这个问题的最好方法是允许卸载类,这样你的 PermGen 就永远不会用完:

-XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled

Stuff like that worked magic for me in the past.过去这样的事情对我来说很神奇。 One thing tho, there's a significant performance tradeoff in using those, since permgen sweeps will make like an extra 2 requests for every request you make or something along those lines.有一件事,在使用这些时有一个显着的性能权衡,因为 permgen 扫描会为您提出的每个请求或类似的东西产生额外的 2 个请求。 You'll need to balance your use with the tradeoffs.您需要在使用与权衡之间取得平衡。

You have to allocate more space to the PermGenSpace of the tomcat JVM.您必须为 tomcat JVM 的 PermGenSpace 分配更多空间。

This can be done with the JVM argument : -XX:MaxPermSize=128m这可以通过 JVM 参数来完成: -XX:MaxPermSize=128m

By default, the PermGen space is 64M (and it contains all compiled classes, so if you have a lot of jar (classes) in your classpath, you may indeed fill this space).默认情况下,永久代空间为 64M(并且它包含所有已编译的类,因此如果您的类路径中有很多 jar(类),您可能确实会填满这个空间)。

On a side note, you can monitor the size of the PermGen space with JVisualVM and you can even inspect its content with YourKit Java Profiler附带说明一下,您可以使用JVisualVM监控 PermGen 空间的大小,甚至可以使用YourKit Java Profiler检查其内容

为了补充迈克尔的回答,根据这个回答,处理这个问题的安全方法是使用以下参数:

-XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled -XX:+UseConcMarkSweepGC

If tomcat is running as Windows Service neither CATALINA_OPTS nor JAVA_OPTS seems to have any effect.如果 tomcat 作为 Windows 服务运行,则 CATALINA_OPTS 和 JAVA_OPTS 似乎都没有任何效果。

You need to set it in Java options in GUI.您需要在 GUI 中的 Java 选项中设置它。

The below link explains it well下面的链接解释得很好

http://www.12robots.com/index.cfm/2010/10/8/Giving-more-memory-to-the-Tomcat-Service-in-Windows http://www.12robots.com/index.cfm/2010/10/8/Giving-more-memory-to-the-Tomcat-Service-in-Windows

In Tomcat 7.0 Windows Service Installer Version.There is not catalina.bat in /bin .在 Tomcat 7.0 Windows 服务安装程序版本中。 /bin 中没有 catalina.bat 。 So you need open Tomcat7w.exe in /bin and add blow JVM argument所以你需要在/bin中打开Tomcat7w.exe并添加blow JVM参数

-XX:PermSize=256m -XX:MaxPermSize=512m

on Java Option in Java Tab, like this.在 Java 选项卡中的 Java 选项上,像这样。 You also add other options.您还可以添加其他选项。

在此处输入图片说明

Another, if you use IntellijIDEA you need add JVM argument in Server Configurations,like this.另外,如果您使用 IntellijIDEA,则需要在服务器配置中添加 JVM 参数,就像这样。

在此处输入图片说明

This one worked for me, in startup.bat the following line needs to be added if it doesn't exist set JAVA_OPTS with the value -Xms128m -Xmx1024m -XX:PermSize=64m -XX:MaxPermSize=256 .这个对我-Xms128m -Xmx1024m -XX:PermSize=64m -XX:MaxPermSize=256 ,在startup.bat ,如果它不存在,则需要添加以下行,将set JAVA_OPTS-Xms128m -Xmx1024m -XX:PermSize=64m -XX:MaxPermSize=256 The full line:全线:

set JAVA_OPTS=-Dfile.encoding=UTF-8 -Xms128m -Xmx1024m -XX:PermSize=64m -XX:MaxPermSize=256

@AndreSmiley 's code of line worked for me. @AndreSmiley 的代码对我有用。

only modification required is.唯一需要修改的是。

-XX:MaxPermSize=256 m -XX:MaxPermSize=256 m

"m" means MB. “m”表示MB。

Actually my application is kinda huge so i was advised to make it 1024m for performance.实际上,我的应用程序有点庞大,因此建议我将其设置为 1024m 以提高性能。

If your using eclipse with tomcat follow the below steps如果您在 tomcat 中使用 eclipse,请按照以下步骤操作

  1. On server window Double click on tomcat, It will open the tomcat's Overview window .在服务器窗口双击 tomcat,它会打开 tomcat 的概览窗口。

  2. In the Overview window you will find Open launch configuration under General information and click on Open launch configuration .在概览窗口中,您将在常规信息下找到打开启动配置,然后单击打开启动配置

  3. In the edit Configuration window look for Arguments and click on It.在编辑配置窗口中查找参数并单击它。
  4. In the arguments tag look for VM arguments .在 arguments 标签中查找VM arguments
  5. simply paste this -Xms256m -Xmx512m -XX:PermSize=256m -XX:MaxPermSize=512m to the end of the arguments.只需将此-Xms256m -Xmx512m -XX:PermSize=256m -XX:MaxPermSize=512m粘贴到参数的末尾即可。

编辑启动配置属性

I'm running tomcat7 on CentOS 6.6.我在 CentOS 6.6 上运行 tomcat7。 I tried creating a new /usr/share/tomcat/bin/setenv.sh file and setting both JAVA_OPTS and CATALINA_OPTS .我尝试创建一个新的 /usr/share/tomcat/bin/setenv.sh 文件并设置JAVA_OPTSCATALINA_OPTS But tomcat wouldn't pick up the values on restart.但是 tomcat 不会在重新启动时获取这些值。 When I put the following line into /etc/tomcat/tomcat.conf:当我将以下行放入 /etc/tomcat/tomcat.conf 时:

CATALINA_OPTS="-Xms128m -Xmx1024m -XX:MaxPermSize=1024m"

tomcat did pick up the new environment variable. tomcat 确实选择了新的环境变量。 I verified this by running我通过运行验证了这一点

ps aux | ps辅助| grep tomcat猫猫

and seeing the new settings in the output.并在输出中查看新设置。 This took a long time to diagnose and I didn't see anyone else suggesting this, so I thought I'd throw it out to the internet community.这花了很长时间来诊断,我没有看到其他人提出这个建议,所以我想我会把它扔给互联网社区。

I tried the same on Intellij Ideav11.我在 Intellij Ideav11 上尝试了同样的方法。

It was not picking up the settings after checking the process using grep.使用 grep 检查过程后,它没有选择设置。 In case it does not, give the mem settings for JAVA_OPTS in catalina.sh instead.如果没有,请在 catalina.sh 中为 JAVA_OPTS 提供内存设置。

Killing the tomcat process(forcefully, kill -9 for Linux) and starting it again solves the issue.杀死 tomcat 进程(强行杀死 -9 对于 Linux)并再次启动它可以解决问题。 My guess is, the tomcat instance doesn't get killed properly using shutdown.bat.我的猜测是,使用shutdown.bat 没有正确杀死tomcat 实例。 So, we need to forcefully kill the process and start again.所以,我们需要强行终止进程并重新开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM