简体   繁体   English

Java内存泄漏迹象

[英]Java Memory leakage signs

i've been making an application and i've been tracking its memory usage. 我一直在做一个应用程序,我一直在跟踪它的内存使用情况。 The app's memory is about 58,676 K as seen below. 应用程序的内存大约为58,676 K,如下所示。 在此输入图像描述

This is my GUI. 这是我的GUI。

在此输入图像描述

That close button there has a function that makes that panel not Visible. 那个关闭按钮有一个功能,使该面板不可见。

private final ActionListener closeButtonAL = new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent e) {
        timeUtilities.getPanel().setVisible(false);
    }

};

I also have a button that makes the panel visible again. 我还有一个按钮,使面板再次可见。

private final ActionListener showPanelAL = new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent e) {
        timeUtilities.getPanel().setVisible(true);
    }

};

The panel has been initialized already before the app starts showing. 该面板已在应用程序开始显示之前初始化。

My problem is that, by just making the GUI appear and closes it again and again increases the memory usage of the application. 我的问题是,只需使GUI出现并再次关闭它就可以再次增加应用程序的内存使用量。

Isn't this some sort of memory leak? 这不是某种内存泄漏吗? Regardless of the answer, how do I prevent this matter? 无论答案如何,我该如何预防此事?

在此输入图像描述

No that may not be a sign of memory leak. 不,这可能不是内存泄漏的迹象。 Because when you close and open the gui multiple times your app may be creating and discarding some objects. 因为当您关闭并多次打开gui时,您的应用可能会创建并丢弃某些对象。 But JVM may not have garbage collected them yet. 但是JVM可能还没有垃圾收集它们。 If you want the real picture use a profiling tool. 如果你想要真实的图片使用分析工具。 Take heap dumps over time after the GC runs. GC运行后,随着时间的推移进行堆转储。 This will tell you what is still remaining in the memory. 这将告诉你内存中还剩下什么。

This can help you narrow down your hunt for a possible memory leak. 这可以帮助您缩小寻找可能的内存泄漏的范围。

Simplest tools to profile. 最简单的工具来分析。

  1. Netbeans comes with a built in profiler. Netbeans带有一个内置的分析器。
  2. Jconsole can also help a bit Jconsole也可以帮助一点
  3. VisualVm can also aid a bit. VisualVm也可以帮助一点。

Also see this link how to analyze heapdumps 另请参阅此链接如何分析heapdumps

You should use a profiler to locate your memory leak. 您应该使用分析器来查找内存泄漏。

In NetBeans, at the top next to "Run Project" and "Debug Project" buttons is a "Profile Project" button (Alt-F2). 在NetBeans中,“运行项目”和“调试项目”按钮旁边的顶部是“配置文件项目”按钮(Alt-F2)。

First run, it will may ask you to calibrate or something. 首次运行时,它可能会要求您进行校准或其他操作。

Afterwars, you can choose to analyse CPU or Memory. Afterwars,您可以选择分析CPU或内存。 If you click on Memory, check "Simple," and click Run, you can run your project and see what's using memory. 如果单击Memory,选中“Simple”,然后单击Run,您可以运行项目并查看使用内存的内容。

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

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