Memory leak in a Java web application

后端 未结 8 1759
借酒劲吻你
借酒劲吻你 2021-01-31 19:24

I have a Java web application running on Tomcat 7 that appears to have a memory leak. The average memory usage of the application increases linearly over time when under load (d

相关标签:
8条回答
  • 2021-01-31 20:14

    The plateau is caused by the available memory dropping below the default percentage threshold which causes a Full GC. This explains why the performance drops as the JVM is constantly pausing while it tries to find and free memory.

    I would usually advise to look at object caches but in your case I think your Heap size is simply too low for a Tomcat instance + webapp. I would recommend increasing your heap to 1G (-Xms1024m -Xmx1024m) and then review your memory usage again.

    If you still see the same kind of behaviour then you should take another Heap dump and look at the largest consumers after String and Char. It my experience this is usually caching mechanisms. Either increase your memory further or reduce the caching stores if possible. Some caches only define number of objects so you need to understand how big each cached object is.

    Once you understand your memory usage, you may be able to lower it again but IMHO 512MB would be a minimum.

    Update:

    You need not worry about unreachable objects as they should be cleaned up by the GC. Also, it's normal that the largest consumers by type are String and Char - most objects will contain some kind of String so it makes sense that Strings and Chars are the most common by frequency. Understanding what holds the objects that contains the Strings is the key to finding memory consumers.

    0 讨论(0)
  • 2021-01-31 20:15

    As this sounds unspecific, one candidate would have been JSF. But then I would have expected hash maps leaking too.

    Should you use JSF: In web.xml you could try:

    • javax.faces.STATE_SAVING_METHOD client
    • com.sun.faces.numberOfViewsInSession 0
    • com.sun.faces.numberOfLogicalViews 1

    As for tools: JavaMelody might be interesting for continual statistics, but needs effort.

    0 讨论(0)
提交回复
热议问题