处理大型Java项目中的内存泄漏的最佳实践? [英] Best practice for handling memory leaks in large Java projects?

查看:128
本文介绍了处理大型Java项目中的内存泄漏的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我参与过的几乎所有大型Java项目中,我都注意到应用程序的服务质量随着容器的正常运行时间而降低。这很可能是由于代码中的内存泄漏造成的。

In almost all larger Java projects I've been involved with I've noticed that the quality of service of the application degrades with the uptime of the container. This is most probably due to memory leaks in the code.

解决此问题的正确方法显然是追溯问题的根本原因并修复泄漏在代码中。解决问题的快速而简单的方法就是重启Tomcat(或者你正在使用的任何servlet容器)。

The correct way to solve this problem is obviously to trace back to the root cause of the problem and fix the leaks in the code. The quick and dirty way of solving the problem is simply restarting Tomcat (or whichever servlet container you're using).

这是我的三个问题:


  • 假设您选择通过跟踪问题的根本原因(内存泄漏)来解决问题,您将如何收集数据以放大关于这个问题?

  • Assume that you choose to solve the problem by tracing the root cause of the problem (the memory leaks), how would you collect data to zoom in on the problem?

假设您只需重新启动容器即可选择快速而肮脏的加速方式,如何收集数据以选择最佳重启周期?

Assume that you choose the quick and dirty way of speeding things up by simply restarting the container, how would you collect data to choose the optimal restart cycle?

您是否能够在很长一段时间内部署和运行项目而无需重新启动servlet容器以重新获得快速恢复?或者偶尔会重新启动一个必须简单接受的servlet?

Have you been able to deploy and run projects over an extended period of time without ever restarting the servlet container to regain snappiness? Or is an occasional servlet restart something that one has to simply accept?

推荐答案


假设您选择通过跟踪问题的根本原因(内存泄漏)来解决问题,您将如何收集数据以放大问题?

Assume that you choose to solve the problem by tracing the root cause of the problem (the memory leaks), how would you collect data to zoom in on the problem?

使用 jmap 并使用 Eclipse Memory Analyzer 。从那里你可以分析哪些对象正在占用最多的内存,哪些根阻止其他对象被收集等等。

Take a heap dump using jmap and load the dump using Eclipse Memory Analyzer. From there you can analyze which objects are eating up the most memory, which "roots" are preventing other objects from being collected, etc.

还有其他堆分析程序,例如 jhat ,但我发现EMA是最快和最好的(免费)解决方案。

There are other heap analysis programs, such as jhat, but I've found EMA to be the fastest and best (free) solution.


假设你只需重新启动容器就可以选择快速而又脏的方式来加速,你会如何收集数据以选择最佳的重启周期?

Assume that you choose the quick and dirty way of speeding things up by simply restarting the container, how would you collect data to choose the optimal restart cycle?

使用JMX监视堆大小以及其他堆和GC统计信息。

Use JMX to monitor heap size and other heap and GC statistics.


您是否能够在较长时间内部署和运行项目没有重新启动servlet容器以恢复快速的时间?

Have you been able to deploy and run projects over an extended period of time without ever restarting the servlet container to regain snappiness?

是的。通过避免/修复内存泄漏。

Yes. By avoiding/fixing memory leaks.

这篇关于处理大型Java项目中的内存泄漏的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆