Howdy. I’m making a display intended to be constantly running on a TV as a sort of information Plaque.
I utilize PImage objects as I have a folder of pictures I wish to display as a slideshow, and I am aware that those objects tend to take up a lot of memory.
My program “resets” at midnight every day, meaning it scraps all of the elements I have created and creates new ones, along with fetching the necessary information from online etc. All of it works fine except my array of PImages. The previously used images, even when overwritten and attempted to be collected by the almighty GC, persist in memory and clog up my RAM. After even two or three resets, I exceed my allocated memory (2 whole-a** GBs), and I get a heap overflow exception.
I have researched quite a bit, tried to call every collection “suggester” (g.removeCache(PImage), System.gc(), and setting the objects to NULL), but the issue persists.
Is there a fix for this currently? Am I making a terrible rookie mistake? Any help is immensely appreciated
Probably if you’re loading all images at once! You’d be better using requestImage() with possibly two PImage fields (current and next) to load the images on demand.
If you ever need System.gc() in finished code you’re almost certainly doing something wrong.
I had thought about that early on, seemed like the most sensible way to minimize memory usage, but that proved to be quite processor intensive throughout runtime, not just in setup() and init(), where the heavy functions would usually be executed.
That being said, I believe that might be the best way to go about things, so I’ll try my hand at your method as well. Cheers for the suggestion <3
–Edit: I may have spoken too soon because I had never used requestImage() instead of loadImage() seems like a much better option for this setting and it negates my concern
As you’re already using VisualVM, if you haven’t already, use the heap dump option under Sampler / Memory; find instances that shouldn’t still be there, and right-click find GC root. That’ll tell you what’s keeping hold of references.
Memory issues can also be caused by native memory, although usually only with the OpenGL renderer or other native library - that memory is free’d by the garbage collector, but as it’s not part of the Java heap it doesn’t trigger garbage collection.