简体   繁体   中英

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.

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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