# PImage garbage not being properly collected

**URL:** <https://discourse.processing.org/t/pimage-garbage-not-being-properly-collected/12252>\
**Category:** Coding Questions\
**Created:** [June 23, 2019, 6:00pm UTC](https://discourse.processing.org/t/pimage-garbage-not-being-properly-collected/12252 "2019-06-23T18:00:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![n055](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/n055/32/5466_2.png) [@n055](https://discourse.processing.org/u/n055)\
**Post date:** [June 23, 2019, 6:00pm UTC](https://discourse.processing.org/t/pimage-garbage-not-being-properly-collected/12252/1 "2019-06-23T18:00:14Z")

</div>

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 🙂

Here’s the project if that would aid in your suggestions [https://github.com/nossonCotlar/Plaque](https://github.com/nossonCotlar/Plaque)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 24, 2019, 12:09am UTC](https://discourse.processing.org/t/pimage-garbage-not-being-properly-collected/12252/2 "2019-06-24T00:09:20Z")

</div>

This seems to run stable without “running out of memory”:

1. In Processing “Preferences” I set maximum available memory to 2048 MB.

2. keyPressed() modified so it would do the same as your midnight “reset” for testing.

3. I added a “flag” to keyPressed() so it waited 10 secs before next key press.; otherwise it will fill memory.

4. Also added a timer to “reset” every 15 secs. I will see how long it runs and update later…

I used VisualVM 1.4.3 to monitor heap size and used heap.  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a6f597e762382467b96dc90030cf54b1e1862b1a.png)

Code only showing what was modified:

```auto
boolean stop = false;
boolean flag = false;
long time;
long timeWait = 10000;

void setup() {
// Same
}

void draw() {
//Same

  updateCheck();

  if (time+timeWait <= millis()) flag = true;
  println(flag);

// timer
  if (time+15000 <= millis())
    {
    freeStuffUp();
    thread("init");
    time = millis();
    flag = false;
    }

  println(mouseX, ' ', mouseY);
  //println(frameRate);
  
  if (time+timeWait <= millis()) flag = true;
  println(flag);
// System.gc();
  }

void keyPressed() {
  if (key == 'p') saveFrame("save.png"); 
  if (key == 'r' && flag == true) {
    frameCount = -1;
    freeStuffUp();
    flag = false;
    time = millis();
    thread("init");
  }
}

```

I had similar issues in the past and wanted to see what I could discover.  
I did not spend too much time on this…

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 24, 2019, 12:23am UTC](https://discourse.processing.org/t/pimage-garbage-not-being-properly-collected/12252/3 "2019-06-24T00:23:12Z")

</div>

After 15 minutes and resetting every 15 secs:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b9e4dd3b16153aa9017241d81cb8855d95f4ccfc.png)

Thank you for sharing the code!  
I learned a lot going through it.

🙂

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 24, 2019, 12:55am UTC](https://discourse.processing.org/t/pimage-garbage-not-being-properly-collected/12252/4 "2019-06-24T00:55:24Z")

</div>

Another exploration with less memory:

1. In Processing “Preferences” I set maximum available memory to **1024 MB**.
2. keyPressed() modified so it would do the same as your midnight “reset” for testing.

It crashed on a key press:

```auto
void keyPressed() {
  if (key == 'p') saveFrame("save.png"); 
  if (key == 'r') {
    frameCount = -1;
    freeStuffUp();
    thread("init"); \\ Added this!
  }

```

You can see where it crashed at end of image:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/7a6381c87d91428231db13d5c5715861455e9a86.png)

Same settings as above but with the addition of System.gc() at end of draw():

```auto
  println(mouseX, ' ', mouseY);
  //println(frameRate);

  System.gc();
}

```

And voila! It would only crash if I pressed it during a peak cycle of the “used heap” otherwise seemed to run fine; I would use more memory!

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/363fea34dba2eb88324f048554bb1d48d0bb4745.png)

🙂

---

<div class="post-metadata">

**Author:** ![n055](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/n055/32/5466_2.png) [@n055](https://discourse.processing.org/u/n055)\
**Post date:** [June 24, 2019, 9:48am UTC](https://discourse.processing.org/t/pimage-garbage-not-being-properly-collected/12252/5 "2019-06-24T09:48:45Z")

</div>

Thanks for your time @glv!! This really helps a lot, I will implement your changes. And I’m glad you learned from the code haha no worries 🙂 Take care

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [June 24, 2019, 10:10am UTC](https://discourse.processing.org/t/pimage-garbage-not-being-properly-collected/12252/6 "2019-06-24T10:10:21Z")

</div>

> [@n055](#):
>
> 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?

Probably if you’re loading all images at once! You’d be better using [requestImage()](https://processing.org/reference/requestImage_.html) 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.

---

<div class="post-metadata">

**Author:** ![n055](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/n055/32/5466_2.png) [@n055](https://discourse.processing.org/u/n055)\
**Post date:** [June 24, 2019, 10:29am UTC](https://discourse.processing.org/t/pimage-garbage-not-being-properly-collected/12252/7 "2019-06-24T10:29:24Z")

</div>

> [@neilcsmith](#):
>
> You’d be better using [requestImage()](https://processing.org/reference/requestImage_.html) with possibly two PImage fields (current and next) to load the images on demand.

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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 24, 2019, 4:26pm UTC](https://discourse.processing.org/t/pimage-garbage-not-being-properly-collected/12252/8 "2019-06-24T16:26:49Z")

</div>

I had related issues a year ago with a growing heap:

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/26651/pshape-rotations-and-translations-increase-memory-used-used-heap-and-heap-size-with-time)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

And this what it does over time until it crashes:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/ddc39a96aba9b45930e5cf7549c763de2c5e2116.png)

Please share if you learn anything new.

🙂

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [June 24, 2019, 5:48pm UTC](https://discourse.processing.org/t/pimage-garbage-not-being-properly-collected/12252/9 "2019-06-24T17:48:11Z")

</div>

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.
