简体   繁体   English

处理JFrame会导致内存泄漏吗?

[英]Does disposing a JFrame cause memory leakage?

I am writing a test program as follows: 我正在编写一个测试程序如下:

  1. When a user clicks button A, it opens 50 JFrames. 当用户单击按钮A时,它会打开50个JFrame。
  2. When the user clicks button B it disposes all JFrames shown by clicking button A. 当用户单击按钮B时,它会通过单击按钮A来处置所有显示的JFrame。

I find that the memory does not decrease after clicking button B. I determined this using the Task Manager, ctrl + alt + del in Windows, and checking the memory usage of "java". 我发现单击按钮B后内存不会减少。我使用任务管理器确定了这个,在Windows中使用ctrl + alt + del ,并检查“java”的内存使用情况。

That's right, no way, not able to solve that (not only in Java PL), 这是对的,没办法,不能解决这个问题(不仅仅是在Java PL中),

1) really don't create lots of Top-Level Containers on Runtime/Fly, because they are never finalized, and until current JVM instance exits, and these Object never been GC'ed only their Grapfics2D 1)真的不会在Runtime / Fly上创建大量的顶级容器 ,因为它们永远不会被最终确定,并且直到当前的JVM实例退出,并且这些Object从未被Grapfics2DGrapfics2D他们的Grapfics2D

2) myContainer#dispose() on Runtime is same for current JVM instance as myContainer#setVisible(false) in connections with JVM available and used Memory 2)运行时的myContainer#dispose()对于当前JVM实例是相同的,因为myContainer#setVisible(false)与JVM连接可用和使用内存

3) create only few Top-Level Containers (maximum simultaneously displayed ), re-use that, but put there JPanel as 1.st JComponent and call myPanel#removeAll() , otherwise you'll remove RootPane and from your Container stays only Borders :-) would be translucent 3)只创建几个Top-Level Containers (最多同时显示),重新使用它,但把JPanel放在1.st JComponent并调用myPanel#removeAll() ,否则你将删除RootPane并从你的Container只留下边框:-)将是半透明的

4) partialy is possible to reduce JVM used Memory by call GC, but just returs amount from Graphics2D and Garbage doesn't works immediatelly, 4)部分可以通过调用GC减少JVM使用的内存,但只是从Graphics2DGarbage中重新调整数量不能立即起作用,

5) more here usefull info here 5)更在这里有用的信息在这里

Without any code we can't help you much... are you calling jFrame. 没有任何代码我们无法帮助你...你在调用jFrame吗? dispose() ? dispose()

public void dispose() public void dispose()

Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. 释放此Window,其子组件及其所有子组件使用的所有本机屏幕资源。 That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable. 也就是说,这些组件的资源将被销毁,它们使用的任何内存都将返回给操作系统,并且它们将被标记为不可显示。

More information available here 更多信息请点击此处

If there is a strong reference to GC(garbage collector) root from your frame, then if you called dispose method, it does not get garbage collected, so you can't see any memory changes after you dispose all of the frames. 如果你的帧中有一个对GC(垃圾收集器)根的强引用,那么如果你调用dispose方法,它就不会被垃圾收集,因此在你处理完所有帧之后你看不到任何内存变化。 If you want memory, from stuff that you should release, then you need to garbage collect the frames. 如果你想要内存,从你应该释放的东西,那么你需要垃圾收集帧。

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

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