1

In my application I cache all images during startup in a Map that looks somewhat like this:

Map<String, javafx.scene.image.Image> imageCache;

While profiling my application I noticed this map being the largest object in my application (~150MB). Which lead me to consider if it is at all necessary:

Images that are used as part of the user interface, do not need to be cached, as they are referenced from the UI.

Large images (1920x1920) may take a while until they are loaded and they usually need to be processed based on the window size before being displayed. These type of images are only used in one place (not referenced multiple times, not counting the cache), but over time they may be displayed multiple times (added and then removed from the scene graph).

In the future there will be small images that are used multiple times in the UI. Here it seems to make sense, to reuse the same Image instance to reduce the memory footprint.

What is a good approach to this? Forgo caching altogether? Caching only specific images? If yes what should be the criteria?

I am aware that my description is a bit abstract, so if you want some clarification on certain points, please ask.

hotzst
  • 7,238
  • 9
  • 41
  • 64
  • Just a question, you would not have imported something wrong? i think your Map should look like this: `Map imageCache` – Bo Halim Jan 18 '17 at 19:37
  • 1
    I put it that way to show the complete type of `Image`. – hotzst Jan 18 '17 at 19:44
  • There's probably no single good answer to this, but I think I would cache images that are used multiple times at the same resolution (particularly if small). Note that you can resize images on loading (not just when you display them), so you can cache the resized version. – James_D Jan 18 '17 at 19:48
  • I had also performance problem: I have used ehcache to cache Image (you have many cache policies, it is better than using a hashmap) But I also managed to load Image in another thread (not in javaFx graphical thread). – pdem Jan 19 '17 at 12:56
  • I think `Node` class cache property and `snaphsot` method could help. See a relevant answer: http://stackoverflow.com/a/18911838/387048 – Omid Jan 19 '17 at 14:22

0 Answers0