简体   繁体   English

如何检查内存泄漏?

[英]How to check memory leak?

Is it possible to check the memory leak without going into the code. 是否可以在不进入代码的情况下检查内存泄漏。 I have the application with me and I want to check whether there is memory leak or not. 我有我的应用程序,我想检查是否有内存泄漏。

In my present organisation, I check the cpu usage before and after running the application as well as the cpu usage of the process of the application. 在我目前的组织中,我检查运行应用程序之前和之后的cpu使用情况以及应用程序进程的cpu使用情况。 But I don't think this is the correct method. 但我不认为这是正确的方法。

Please advise me in this regard. 在这方面请告诉我。

If you run your Java application with the following flag: 如果使用以下标志运行Java应用程序:

-Dcom.sun.management.jmxremote

You will be able to connect to it with jconsole . 您将能够使用jconsole连接到它。 jconsole is a tool that comes with Java and resides in the bin directory where the java program is found. jconsole是一个Java附带的工具,位于找到java程序的bin目录中。 You can run it and observe memory usage over time (it's not great, but can help in spotting leaks). 您可以运行它并观察内存使用情况(它不是很好,但可以帮助发现泄漏)。 On very recent versions of Java (late builds of 1.6) you can also run jvisualvm which is similar to jconsole but will also tell you how many of each class is instantiated, which is more useful. 在最新版本的Java(1.6的后期版本)中,您还可以运行类似于jconsole jvisualvm ,但也会告诉您实例化每个类的数量,这更有用。

I think you need a memory profiler to do this. 我认为你需要一个内存分析器来做到这一点。

You can find a list of profilers here 您可以在此处找到分析器列表

Open Source Profilers for Java 适用于Java的开源Profiler

also read this article on 还阅读了这篇文章

Finding Memory Leaks in Java Apps 查找Java应用程序中的内存泄漏

You are right that watching only the memory is not really easy with Java as it involves a garbage collector and would have a kind of saw pattern. 你是对的,因为它涉及一个垃圾收集器并且会有一种锯模式,因此只能用Java来观察内存并不容易。 Nevertheless could the recording of memory consumption of the whole application over a long time be useful. 然而,长时间记录整个应用程序的内存消耗是有用的。

If you have some automatic mean to "remote control" the application (Like SendKeys under windows) you could make nice time memory consumption diagrams. 如果您有一些自动意味着“远程控制”应用程序(如Windows下的SendKeys),您可以制作美好的时间内存消耗图表。 Use your favorite table calculation program to draw a linear interpolation of the data. 使用您喜欢的表格计算程序绘制数据的线性插值。 If this shows up a regular upwards trend, you could say that there is a leak. 如果这显示出常规的向上趋势,您可以说存在泄漏。 Only do this in a state where the application is not supposed to grow in memory, which can also be done with healthy human thinking about what information an application would need to store to display/process the data. 只有在应用程序不应该在内存中增长的状态下才能执行此操作,这也可以通过健康的人类思考应用程序需要存储哪些信息来显示/处理数据。

Of course other tools are needed to drill down to threads or classes (see other responses). 当然,需要其他工具来深入到线程或类(请参阅其他响应)。

Analysing the memory consumption of a Java application should not be done by using OS tooling, IMHO. 不应该使用OS工具,恕我直言来分析Java应用程序的内存消耗。 A running JVM will allocate an amount of memory and is unlikely to release it, even if it is not actually needed by the JVM at a given point in time. 正在运行的JVM将分配一定量的内存,并且不太可能释放它,即使JVM在给定的时间点实际上并不需要它。 An OS tool will only report the amount of memory that was allocated by the JVM, not the amount of memory that is actually committed within the JVM. OS工具仅报告JVM分配的内存量,而不是JVM中实际提交的内存量。

The tooling provided with the JDK (jstat, jconsole, jvisualvm) is much more reliable. 随JDK(jstat,jconsole,jvisualvm)提供的工具更加可靠。 When interpreting memory usage, the most important thing is the size of the actual heap. 在解释内存使用时,最重要的是实际堆的大小。 A Java application will typically display a sawtooth pattern. Java应用程序通常会显示锯齿图案。 The amount of heap in use will rise gradually over time, and drop off sharply when GC frees heap space by removing all the objects that are no longer needed. 随着时间的推移,堆中使用的堆将逐渐增加,并且当GC通过删除不再需要的所有对象来释放堆空间时,堆将急剧下降。

A definite warning signal is a sawtooth that is slowly ascending: the sharp drop caused by the GC is ending a little higher every time. 一个明确的警告信号是一个缓慢上升的锯齿:由GC引起的急剧下降每次都会结束一点点。 If the application runs for a long time (typically for a server application), this will most probably cause an OutOfMemory error in the long run. 如果应用程序运行很长时间(通常用于服务器应用程序),这很可能会导致长期运行中的OutOfMemory错误。

Another thing to wach for is a sawtooth where the 'teeth' are getting sharper and higher. 另一件要做的事情是锯齿,其中“牙齿”越来越尖锐。 This also indicates that the application will need more and more memory over time. 这也表明应用程序随着时间的推移需要越来越多的内存。

If you want to analyse the root casue of a perceived problem you need to analyse the number of objects created and have a look at how long they live. 如果要分析感知问题的根源,您需要分析创建的对象数量并查看它们存在多长时间。 This is not trivial stuff. 这不是一件小事。

如果它的unix c代码然后使用valgrind

Yes it is possible to check memory leaks. 是的,可以检查内存泄漏。 Use Perf tool to check the leaks. 使用Perf工具检查泄漏。

A sample usage of probes with perf could be to check libc's malloc() and free() calls: 使用perf的探针的示例用法可以是检查libc的malloc()和free()调用:

$ perf probe -x /lib64/libc.so.6 malloc $ perf probe -x /lib64/libc.so.6 malloc

$ perf probe -x /lib64/libc.so.6 free $ perf probe -x /lib64/libc.so.6免费

Added new event: probe_libc:malloc (on 0x7eac0) 添加了新事件:probe_libc:malloc(在0x7eac0上)

A probe has been created. 已创建探针。 Now, let's record the global usage of malloc and free across all the system during 4 second: 现在,让我们记录4秒内所有系统中malloc和free的全局使用情况:

$ perf record -e probe_libc:malloc -agR sleep 4 $ perf记录-e probe_libc:malloc -agR sleep 4

$ perf record -e probe_libc:free -agR sleep 4 $ perf记录-e probe_libc:free -agR sleep 4

Let's record the usage of malloc and free across all any process during 4 second: 让我们在4秒内记录malloc和free的使用情况:

$ perf stat -e probe_libc:free -e probe_libc:malloc -ag -p $(pgrep $process_name$) sleep 4 $ perf stat -e probe_libc:free -e probe_libc:malloc -ag -p $(pgrep $ process_name $)sleep 4

Output: 输出:

Performance counter stats for process id '1153': 进程ID“1153”的性能计数器统计信息:

11,312 probe_libc:free 11,312 probe_libc:免费

11,644 probe_libc:malloc 11,644 probe_libc:malloc

4.001091828 seconds time elapsed 4.001091828秒时间过去了

If there is increase in difference between malloc and free count for every time perf command is run, it is a hint of memory leak. 如果每次运行perf命令时malloc和free count之间的差异有所增加,那就是内存泄漏的暗示。

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

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