简体   繁体   English

Tomcat修复内存泄漏?

[英]Tomcat Fix Memory Leak?

I am using 6.0.20 I have a number of web apps running on the server, over time, approximately 3 days and the server needs restarting otherwise the server crashes and becomes unresponsive. 我正在使用6.0.20我在服务器上运行了许多Web应用程序,随着时间的推移,大约3天,服务器需要重新启动,否则服务器崩溃并变得无响应。

I have the following settings for the JVM: 我有JVM的以下设置:

-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20\logs

This provides me with a hprof file which I have loaded using Java VisualVM which identifies the following: 这为我提供了一个hprof文件,我使用Java VisualVM加载了该文件,该文件标识了以下内容:

byte[] 37,206   Instances | Size 86,508,978
int[] 540,909   Instances | Size 55,130,332
char[] 357,847  Instances | Size 41,690,928

The list goes on, but how do I determine what is causing these issues? 列表继续,但我如何确定导致这些问题的原因?

I am using New Relic to monitor the JVM and only one error seems to appear but it's a reoccurring one, org.apache.catalina.connector. 我正在使用New Relic来监视JVM,似乎只出现了一个错误,但它是一个重复发生的错误,org.apache.catalina.connector。 ClientAbortException. ClientAbortException。 Is it possible that when a user session is aborted, any database connections or variables created are not being closed and are therefore left orphaned? 是否可能在用户会话中止时,创建的任何数据库连接或变量都没有被关闭,因此是孤立的?

There is a function which is used quite heavily throughout each web app, not sure if this has any bearing on the leak: 在每个Web应用程序中都有一个非常重要的功能,不确定这是否与泄漏有关:

public static String replaceCharacters(String s)
{
    s = s.replaceAll("  ", " ");
    s = s.replaceAll(" ", "_");
    s = s.replaceAll("\351", "e");
    s = s.replaceAll("/", "");
    s = s.replaceAll("--", "-");
    s = s.replaceAll("&", "and");
    s = s.replaceAll("&", "and");
    s = s.replaceAll("__", "_");
    s = s.replaceAll("\\(", "");
    s = s.replaceAll("\\)", "");
    s = s.replaceAll(",", "");
    s = s.replaceAll(":", "");
    s = s.replaceAll("\374", "u");
    s = s.replaceAll("-", "_");
    s = s.replaceAll("\\+", "and");
    s = s.replaceAll("\"", "");
    s = s.replaceAll("\\[", "");
    s = s.replaceAll("\\]", "");
    s = s.replaceAll("\\*", "");
    return s;
}

Is it possible that when a user connection is aborted, such as a user browser closed or the users has left the site that all variables, connections, etc... are purged/released, but isn't GC supposed to handled that? 是否有可能在用户连接中止时,例如用户浏览器关闭或用户已离开网站所有变量,连接等等被清除/释放,但GC不应该处理它?

Below are my JVM settings: 以下是我的JVM设置:

-Dcatalina.base=c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20
-Dcatalina.home=c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20
-Djava.endorsed.dirs=c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20\endorsed
-Djava.io.tmpdir=c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20\temp
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20\conf\logging.properties
-Dfile.encoding=UTF-8
-Dsun.jnu.encoding=UTF-8
-javaagent:c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20\newrelic\newrelic.jar
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20\logs
-Dcom.sun.management.jmxremote.port=8086
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false vfprintf
-Xms1024m
-Xmx1536m

Am I missing anything? 我错过了什么吗? The server has 3GB ram. 服务器有3GB内存。

Any help would be much appreciated :-) 任何帮助将非常感激 :-)

... but how do I determine what is causing these issues? ...但我如何确定导致这些问题的原因?

You need to use a dump analyser that allows you to see what is making these objects reachable. 您需要使用转储分析器,它允许您查看使这些对象可访问的内容。 Pick an object, and see what other object or objects refer to it ... and work backwards through the chains until you find either a "GC root" or or some application-specific class that you recognise. 选择一个对象,并查看其他对象或对象引用它...然后通过链向后工作,直到找到“GC根”或您识别的某个特定于应用程序的类。

Here are a couple of references on analysing memory snapshots and memory profilers: 以下是分析内存快照和内存分析器的几个参考:

Once you have identified that, you've gone most of the way to identifying the source of your storage leak. 一旦确定了这一点,您就已经完成了识别存储泄漏源的大部分方法。


That function has no direct bearing on the leak. 该功能与泄漏没有直接关系。 It certainly won't cause it. 它肯定不会导致它。 (It could generate a lot of garbage String objects ... but that's a different issue.) (它可能会生成大量垃圾String对象......但这是一个不同的问题。)

我将所有项目迁移到Tomcat 7.0.42并且我的错误消失了,我们的网站更稳定,速度更快,我们使用更少的内存和cpu使用情况要好得多。

在本地开发环境中启动服务器,附加分析器(最好是你的套件),定期进行堆转储,你会看到对象byte []的增长,你可以实际将那些byte[]与你的应用程序类连接,使用这个工具泄漏它你在代码中存在缺陷

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

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