JavaFX节点对象不被垃圾收集 [英] JavaFX Node objects are not garbage collected

查看:328
本文介绍了JavaFX节点对象不被垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaFX(1.2.x和1.3.x)似乎不允许至少节点和场景的垃圾收集。一个Node对象在从Scene中移除后没有被释放(没有其他明确的引用)。

这里举例说明:

  var buttonB:Button = 
Button {
text:我只是挂在这里
}

var buttonC:Button =
Button {
text:让我泄漏内存
action:function(){
buttonB.managed = false;
从mainBox.content中删除buttonB;
buttonB.skin = null;
buttonB = null;
java.lang.System.gc();
}
}

def mainBox:HBox =
HBox {
hpos:HPos.CENTER
nodeVPos:VPos.CENTER
layoutInfo:LayoutInfo {
width:800 height:600
}
content:[buttonC,buttonB]
}

buttonB永远不会被释放。将皮肤设置为null会有所帮助(在VisualVM中,大多数对按钮的引用都消失了),但并未解决问题。



是否有可能让buttonB符合GC条件,以及如何做到这一点?



这个问题在JavaFX 2.0中持续存在吗?解决方案

1.3会将 SoftReferences 保存到缓冲的图像(这可能表示已被移除的节点的渲染版本的节点)。对我来说,这是一种内存泄漏,因为软引用根据可用内存的数量而被清除。这不是内存泄漏(OutOfMemoryException将不会发生,因为这个),但对我来说这是造成非常低效的垃圾收集的原因。

您可以使用 XX:SoftRefLRUPolicyMSPerMB =< N> 来减少SoftReferences的时间,这是尽管可能(但不太可能)的表现惩罚。它设置一个对象保留的每个空闲MB的毫秒数。默认值为1000毫秒。


JavaFX (1.2.x and 1.3.x) doesn't seem to allow garbage collection for at least Nodes and Scenes. A Node object is not freed after being removed from Scene (there's no other explicit reference to it).

Here goes example:

var buttonB:Button =
   Button {
       text: "i'm just hanging here"
   }

var buttonC:Button =
   Button {
       text: "hit me to leak memory"
       action: function() {
           buttonB.managed = false;
           delete buttonB from mainBox.content;
           buttonB.skin = null;
           buttonB = null;
           java.lang.System.gc();
       }
   }

def mainBox:HBox =
   HBox {
       hpos: HPos.CENTER
       nodeVPos: VPos.CENTER
       layoutInfo: LayoutInfo {
        width: 800 height: 600
       }
       content: [buttonC, buttonB]
   }

buttonB is never freed. Setting skin to null helps somehow (in VisualVM most of the references to the button disappear) but doesn't fix the issue. I also tried nullifying all members using JavaFX reflection with no luck.

Is it possible to make buttonB eligible for GC and how to do it?

Does the problem persist in JavaFX 2.0?

解决方案

I found (through visualVM inspection) that JavaFX 1.3 keeps SoftReferences to buffered images (that probably represent rendered versions of Nodes) for nodes that have been removed. For me this was a sort of memory leak, as soft references are cleared depending on the amount of free memory. This isn't a memory leak (OutOfMemoryException will never happen due to this) but for me this was reason to cause very inefficient garbage collecting.

You can use XX:SoftRefLRUPolicyMSPerMB=<N> to reduce the time SoftReferences are kept, this is at a possible (but unlikely) performance penalty though. It sets the number of milliseconds per free MB that an object is kept. Default is 1000 ms.

这篇关于JavaFX节点对象不被垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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