今天将一个项目部署到linux服务器上,结果tomcat在启动运行到一定时间后就报错。根据以往的经验,查了一些资料,终于解决了该问题并顺便解决了生产环境中的get方式中文传递乱码问题。
tomcat启动内存溢出问题
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: PermGen space
at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:252)
at java.util.concurrent.FutureTask.get(FutureTask.java:111)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1130)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:293)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:727)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.startup.Catalina.start(Catalina.java:646)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:450)
Caused by: java.lang.OutOfMemoryError: PermGen space
at java.lang.StackTraceElement.equals(StackTraceElement.java:204)
at java.lang.Throwable.printEnclosedStackTrace(Throwable.java:688)
at java.lang.Throwable.printStackTrace(Throwable.java:666)
at java.lang.Throwable.printStackTrace(Throwable.java:720)
at java.util.logging.SimpleFormatter.format(SimpleFormatter.java:157)
at java.util.logging.StreamHandler.publish(StreamHandler.java:196)
at java.util.logging.ConsoleHandler.publish(ConsoleHandler.java:105)
at java.util.logging.Logger.log(Logger.java:522)
at java.util.logging.Logger.doLog(Logger.java:543)
at java.util.logging.Logger.logp(Logger.java:743)
at org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:198)
at org.apache.juli.logging.DirectJDKLog.error(DirectJDKLog.java:151)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1132)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:782)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1568)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1558)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722) Aug 11, 2015 10:30:06 AM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: PermGen space
at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:252)
at java.util.concurrent.FutureTask.get(FutureTask.java:111)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1130)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:293)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:727)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.startup.Catalina.start(Catalina.java:646)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:450)
Caused by: java.lang.OutOfMemoryError: PermGen space
at java.lang.StackTraceElement.equals(StackTraceElement.java:204)
at java.lang.Throwable.printEnclosedStackTrace(Throwable.java:688)
at java.lang.Throwable.printStackTrace(Throwable.java:666)
at java.lang.Throwable.printStackTrace(Throwable.java:720)
at java.util.logging.SimpleFormatter.format(SimpleFormatter.java:157)
at java.util.logging.StreamHandler.publish(StreamHandler.java:196)
at java.util.logging.ConsoleHandler.publish(ConsoleHandler.java:105)
at java.util.logging.Logger.log(Logger.java:522)
at java.util.logging.Logger.doLog(Logger.java:543)
at java.util.logging.Logger.logp(Logger.java:743)
at org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:198)
at org.apache.juli.logging.DirectJDKLog.error(DirectJDKLog.java:151)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1132)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:782)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1568)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1558)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
上面的错误表示jvm的堆内存溢出。 PermGen space的全称是Permanent Generationspace,是指内存的永久保存区域,这块内存主要是被JVM存放Class和Meta信息的,Class在被Loader时就会被放到PermGenspace中,它和存放类实例(Instance)的Heap区域不同,GC(GarbageCollection)不会在主程序运行期对PermGenspace进行清理。
要解决以上问题必须采取以下措施
1、将tomcat中各个项目公用的jar包统一放在,tomcat/lib目录下,项目中无需再引用这些包
2、将tomcat中无用的jar包剔除出去。
3、修改tomcat启动的配置参数,主要涉及java虚拟机和tomcat服务器的启动参数
JAVA_OPTS="-server
-Xms1048M
-Xmx1048M
-Xss512k
-XX:+AggressiveOpts
-XX:+UseBiasedLocking
<span style="color:#FF0000;"> -XX:PermSize=512M
-XX:MaxPermSize=512M</span>
-XX:+DisableExplicitGC
-XX:MaxTenuringThreshold=31
-XX:+UseConcMarkSweepGC
-XX:+UseParNewGC
-XX:+CMSParallelRemarkEnabled
-XX:+UseCMSCompactAtFullCollection
-XX:LargePageSizeInBytes=128m
-XX:+UseFastAccessorMethods
-XX:+UseCMSInitiatingOccupancyOnly
-Dfile.encoding=UTF-8
-Djava.awt.headless=true
-Djava.net.preferIPv4Stack=true" CATALINA_OPTS="-server
-Xms1024m
-Xmx1024m
-XX:NewRatio=3
<span style="color:#FF0000;"> -XX:PermSize=512m
-XX:MaxPermSize=512m</span>
-Duser.country=US
-Duser.language=en
-Duser.timezone=Asia/Shanghai
-Dfile.encoding=UTF-8
-Dsun.jnu.encoding=UTF-8
-Dcom.sun.management.jmxremote=true
-Djava.library.path=/usr/local/apr/lib"
修改以上JAVA_OPTS和CATALINA_OPTS中的-XX:PermSize=512m和-XX:MaxPermSize=512m的参数值,第一个桉树表示默认的堆内存大小,第二个参数表示最大的堆内存大小,最好是将这两个值设置一致,减少了内存分配的资源消耗。
生产环境的中文乱码问题
以前在本地中文通过get方式传输,后台接收不会出现乱码问题,但是当项目部署到服务器上后,便出现了乱码问题,今天终于发现是原因是,jdk启动的默认编码是GBK的,所以虚拟机会以GBK方式接收参数,而我的项目是utf-8方式的。因此只需要修改红色部分
JAVA_OPTS="-server
-Xms1048M
-Xmx1048M
-Xss512k
-XX:+AggressiveOpts
-XX:+UseBiasedLocking
-XX:PermSize=128M
-XX:MaxPermSize=512M
-XX:+DisableExplicitGC
-XX:MaxTenuringThreshold=31
-XX:+UseConcMarkSweepGC
-XX:+UseParNewGC
-XX:+CMSParallelRemarkEnabled
-XX:+UseCMSCompactAtFullCollection
-XX:LargePageSizeInBytes=128m
-XX:+UseFastAccessorMethods
-XX:+UseCMSInitiatingOccupancyOnly
<span style="color:#FF6666;"> -Dfile.encoding=UTF-8</span>
-Djava.awt.headless=true
-Djava.net.preferIPv4Stack=true"