简体   繁体   English

Memory Leak and OutOfMemoryException - Java Swing 应用程序

[英]Memory Leak and OutOfMemoryException - Java Swing Application

I am currently working a Standalone Java Swing application, which is quite a complex one with lot of labels and input text fields.我目前正在开发一个独立的 Java Swing 应用程序,这是一个相当复杂的应用程序,有很多标签和输入文本字段。 What I found is, after each operation the memory consumption of the application as in Windows Task manager is increasing atleast by 2 MB.我发现,每次操作后,应用程序的 memory 消耗(如 Windows 任务管理器)至少增加 2 MB。 Upon reaching to a total memory consumption of ~305 MB, the application crashes with Out of Memory Exception.在达到约 305 MB 的总 memory 消耗后,应用程序崩溃并出现 Out of Memory 异常。 I explicitly mentioned for garbage collection in the event handler, but it didn't made any good.我在事件处理程序中明确提到了垃圾收集,但它没有任何好处。 I profiled the application with JVisualVM.我使用 JVisualVM 分析了应用程序。 I found few of my classes have a memory graph of an increment slope which never comes down.我发现我的课程中很少有 memory 图的增量斜率永远不会下降。 InspectableInputTextImpl 类的内存使用图

But for other classes the graph is quite normal like a wave pattern, as below image:但是对于其他类,该图非常正常,就像一个波浪模式,如下图所示: 类 char 数组的内存使用图

From above chart I came to conclusion that memory leak is happening for InspectableInputTextImpl class.从上图我得出结论,InspectableInputTextImpl class 正在发生 memory 泄漏。 To fix I added weakReference instantiation for the said class like below:为了解决这个问题,我为上述 class 添加了 weakReference 实例化,如下所示:

InspectableInputTextImpl inputText = new InspectableInputTextImpl(screen, componentId, length);
        WeakReference<InspectableInputTextImpl> weakInputText = new WeakReference<InspectableInputTextImpl>(inputText);
        ((TInputText) weakInputText.get()).setCustomDocument(new TRestrict4InputTextDocument());
        ((TInputText) weakInputText.get()).setMaxLength(maxLength);
        ((TInputText) weakInputText.get()).setRequirement(required);

Even this also didn't improved anything, still the memory chart looks exactly same.即使这样也没有任何改进,memory 图表看起来仍然完全相同。 Below are my questions:以下是我的问题:

  1. Am I correct on the conclusion that InspectableInputTextImpl itself is causing the memory leak based on the chart?根据图表,我对 InspectableInputTextImpl 本身导致 memory 泄漏的结论是否正确?

  2. If I am correct could you share with your thoughts on fixing the issue.如果我是正确的,您能否分享您对解决此问题的想法。 Will converting the instance to a singleton will fix it?将实例转换为 singleton 会修复它吗?

I would suggest analysing a heap dump to verify the cause of memory leak.我建议分析堆转储以验证 memory 泄漏的原因。 You can start your application with -XX:+HeapDumpOnOutOfMemoryError VM option and it will create a.hprof file with a dump of the heap at the point it crashes.您可以使用 -XX:+HeapDumpOnOutOfMemoryError VM 选项启动您的应用程序,它将在崩溃时创建一个带有堆转储的.hprof 文件。 Then you can use a tool like Eclipse MAT to have a look at the dump.然后您可以使用Eclipse MAT之类的工具来查看转储。 It may verify your suspicion about InspectableInputTextImpl or give you a different clue.它可能会验证您对 InspectableInputTextImpl 的怀疑或为您提供不同的线索。

You can take on how to fix the problem (your question 2) after you verify what exactly is the problem (your question 1).在您确认问题到底是什么(您的问题 1)后,您可以着手解决问题(您的问题 2)。

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

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