gpt4 book ai didi

Javafx 内存泄漏

转载 作者:行者123 更新时间:2023-11-30 11:23:58 27 4
gpt4 key购买 nike

我有一个电话簿应用程序。在开始时,应用程序将所有内容加载到一个 trie 和一个列表中,将它们放在应用程序右侧的 ListView 中,左侧有一些文本字段和 ImageView ,因此当在列表中进行新选择时,这些信息字段已更新为新的联系方式。

像这样: enter image description here

因此在加载并查看所有内容后,实际上并没有再次创建任何内容,我也没有保留任何内容。

When new contact selected I get his data and it is replaced with the previous data in fields even the image I only keep reference to the imag location when contact is selected I go load a new image and put it in the image view :

public void createImage(String url, double loadWidth, double loadHeight, Point2D viewPortStart)
{
notifier.setVisible(false);
//load the image
try{
File file = new File(url);
if(!file.exists())
throw new Exception();

Image image = new Image(file.toURI().toURL().toString(),loadWidth,loadHeight,true,true);

//set dragging fields
this.viewPortStart.x = viewPortStart.getX();
this.viewPortStart.y = viewPortStart.getY();
moved.x = viewPortStart.getX();
moved.y = viewPortStart.getY();

//put image to view
view.setImage(image);

//update viewport
view.setViewport(new Rectangle2D(viewPortStart.getX(),viewPortStart.getY(),SIZE,SIZE));

//notify that this component contains a photo now
containsPhoto.setValue(true);
notifier.setVisible(false);
validProperty().setValue(true);
imageLocation = url;
imageHeight = loadHeight;
imageWidth = loadWidth;
}
catch (Exception ex)
{
createEmptyImage();
showImageLoadError(url == null ? "unknown path":url);
}
}

所以我开始使用 Yourkit 对此进行分析。在应用程序启动时,它在 yourkit 中使用 50mb,在任务管理器中使用 120mb。然后我使用键盘在整个接触列表中向下移动,之后我强制进行垃圾收集,任务管理器上新使用的内存是 400 MB,你的工具包上是 120 MB?!!!

请参阅下方选定的蓝线表示我强制抓取收集的时间。 enter image description here

它表示使用了 120 MB,但当我拍摄快照时,我只看到 15 MB 的保留对象?

enter image description here

谁能帮忙解释一下?或者我如何追踪此处的泄漏?

最佳答案

分配的内存是 JVM 必须使用的内存,而使用的内存是在任何给定时间实际使用的内存量。您可以在 JVM 启动时为分配的内存指定最小值和最大值。最初,JVM 将分配最小值,或者如果您的应用程序需要,则分配更多。如果随着时间的推移使用的内存远低于分配的内存,后者也会下降。 Windows 中的任务管理器通常只报告分配的内存。

如果应用程序未被使用,看到“锯齿”模式是正常的,其中使用的内存在下一次​​垃圾回收之前增长,然后下降到上次 gc 之后的水平。这是正常现象,并不表示存在内存泄漏。

但是,如果每次 gc 后使用的内存量随着时间的推移而增加,则表明存在内存泄漏。

关于Javafx 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20893974/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com