简体   繁体   English

JavaFX图像加载内存泄漏?

[英]JavaFX image loading memory leak?

I'm trying to build an application for "gesture drawing", which is basically just a slideshow of images in set intervals. 我正在尝试为“手势绘图”构建一个应用程序,它基本上只是以设置间隔显示图像的幻灯片。 I noticed that whenever I load a new image that's for instance less than 1MB the memory usage of my app increases by over 20MB, every time a new image loads. 我注意到,每当我加载一个小于1MB的新图像时,每次加载新图像时,我的应用程序的内存使用量都会增加20MB以上。

I've tried two approaches, the first one was with a custom "ImagePane" - in order to be able to resize the image with the window - that loads the given image in its style sheet ( -fx-background-image ), and the second one is using a regular ImageView using the setImage() method. 我尝试了两种方法,第一种方法是使用自定义的“ImagePane” - 为了能够使用窗口调整图像大小 - 在其样式表( -fx-background-image )中加载给定图像,以及第二个是使用setImage()方法使用常规ImageView。 In both cases I noticed significant memory increase. 在这两种情况下,我都注意到内存增加

Now my question is: Is that normal behaviour, a known memory leak or am I doing something wrong? 现在我的问题是:这是正常的行为,已知的内存泄漏还是我做错了什么? I can provide the whole code if necessary, it's just a big prototype mess at the moment though... 如果有必要,我可以提供整个代码,但此刻它只是一个很大的原型混乱...

This is normal behavior. 这是正常行为。 When you load a new image, it will consume approximately 4 bytes per pixel. 加载新图像时,每个像素将占用大约4个字节。 As long as you don't accidentally keep references to images you are no longer using, they will become eligible for garbage collection in the normal way. 只要您不小心保留对不再使用的图像的引用,它们就有资格以正常方式进行垃圾回收。 You have little control over when the garbage collector will reclaim the memory, and you should leave that up to the JVM. 您几乎无法控制垃圾回收器何时回收内存,您应该将其留给JVM。

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

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