# Memory leak in PGraphics

**URL:** <https://discourse.processing.org/t/memory-leak-in-pgraphics/24500>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 10, 2020, 4:41pm UTC](https://discourse.processing.org/t/memory-leak-in-pgraphics/24500 "2020-10-10T16:41:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![NullPointer](https://avatars.discourse-cdn.com/v4/letter/n/b38774/32.png) [@NullPointer](https://discourse.processing.org/u/NullPointer)\
**Post date:** [October 10, 2020, 4:41pm UTC](https://discourse.processing.org/t/memory-leak-in-pgraphics/24500/1 "2020-10-10T16:41:46Z")

</div>

Hi, I am working on a scientific visualization project for my degree and it requires us to draw many circles to the screen representing trees. This we did via the following code which runs as a thread and updates its PGraphic called graphics each time we filter or change zoom. Currently, if I try to re-render by clicking on the screen and moving around it works perfectly but if I do it too quickly and the threads get interrupted (which we allow) ram usage spikes up and we never get it back.

I have tried all sorts of fixes including using removeCache and image.dispose() as well as system.gc().  
The only thing that fixes it is called g.removeCache on the specific graphics and System.gc() every frame (done in draw). This works perfectly but is obviously super inefficient and we drop frames madly.  
Other weird behaviour is if I call only g.removeCache but do it every frame the memory spikes up and down to and from the same places (eg 700mb to 1.2gb in a few seconds and then back down to 700mb). Any help would be appreciated

```
while (true) {
  try {
    if (current) {
      sleep(10);
      //yield();
      continue;
    }

    int currentBuildCount = buildCount.get();
    // drawing loop
    graphics.beginDraw();
    //graphics.scale(1.0 / 2);
    graphics.clear();
    graphics.noStroke();
    graphics.blendMode(ADD);
    graphics.ellipseMode(CENTER);
    for (PlantTree.Node node : plants.filteredPlantsInRect(newPos, zoom.layerPlantRadius(layer).mult(1.1))) {
      color speciesColor = species.colors.get(node.plant.speciesId);
      graphics.fill(speciesColor, 200);
      if (buildCount.get() != currentBuildCount) {
        //System.out.println("Thread restarted!");
        g.removeCache(graphics);
        break;
      }
      // draw current plant
      PVector scaledPlantPos = zoom.plantSpaceToLayerSpace(node.plant.pos, newPos, layer);
      graphics.circle(scaledPlantPos.x, scaledPlantPos.y, max(node.plant.canopyRadius * 2 * zoom.layerScale[layer], 1));
    }
    graphics.endDraw();
    if (buildCount.get() != currentBuildCount) {
      //System.out.println("Thread restarted!");
      g.removeCache(graphics);
      continue;
    }

    // set pos after image is finished
    transferPixels(graphics);
    g.removeCache(graphics);
    graphics.clear();
    this.pos = this.newPos.copy();
    current = true;
  
  }
}

```

}

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [October 10, 2020, 4:58pm UTC](https://discourse.processing.org/t/memory-leak-in-pgraphics/24500/2 "2020-10-10T16:58:58Z")

</div>

It depends how many new PImage calls or create graphics call your making. @GoToLoop will be more knowledgeable on this, but images are only cleared by the java garbage collector, setting something to null does not remove it from memory.

---

<div class="post-metadata">

**Author:** ![NullPointer](https://avatars.discourse-cdn.com/v4/letter/n/b38774/32.png) [@NullPointer](https://discourse.processing.org/u/NullPointer)\
**Post date:** [October 10, 2020, 5:06pm UTC](https://discourse.processing.org/t/memory-leak-in-pgraphics/24500/3 "2020-10-10T17:06:46Z")

</div>

We create 6 PGraphics (one per thread) and we only create them once. We then copy the pixels to a PImage and use that to display it. Once a new layer must be rendered we stop making the thread wait and it calls beginDraw() of the same PGraphic. Doing this, and by clicking again before the thread re-renders we can go from 1gb to 3gb easily. @GoToLoop would appreciate any help on this, have checked many of the other threads you’ve replied to on similar issues but nothing has worked thus far.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 10, 2020, 11:51pm UTC](https://discourse.processing.org/t/memory-leak-in-pgraphics/24500/4 "2020-10-10T23:51:55Z")

</div>

Most I can do is tell you what I know about how a PGraphics caches a PImage.

Processing got 3 methods to display a PImage on a PGraphics:

1. [**background()**](https://processing.org/reference/background_.html)
2. [**set()**](https://processing.org/reference/set_.html)
3. [**image()**](https://processing.org/reference/image_.html)

Both **background()** & **set()** merely transfer a PImage::_pixels[]_ to a PGraphics disregarding any transparency & tint between them; but having the advantage of not being cached.

Method **image()** is more complete, but it caches its PImage argument if it’s not been yet.

Each cache primarily consists of a whole clone of a PImage::_pixels[]_, thus more than doubling memory requirements!

As a general rule prefer reusing a PImage object rather than discarding it, especially if we end up passing it as an **image()** argument.

Be careful about using method PImage::**get()** to make a new clone of it.

Even though the cloned PImage got the same content as its original, method **image()** will consider it a different object, therefore it ends up caching the clone as well!

---

<div class="post-metadata">

**Author:** ![NullPointer](https://avatars.discourse-cdn.com/v4/letter/n/b38774/32.png) [@NullPointer](https://discourse.processing.org/u/NullPointer)\
**Post date:** [October 11, 2020, 7:52am UTC](https://discourse.processing.org/t/memory-leak-in-pgraphics/24500/5 "2020-10-11T07:52:48Z")

</div>

I’ve done some tests and I could be wrong but I believe that the issue is from drawing a circle to the graphic. Each circle is a plant and our largest file has 760,000 plants and so we are drawing many of these and I think they are just being cached so quickly that they can’t be GC’ed. Any ideas?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 11, 2020, 10:45pm UTC](https://discourse.processing.org/t/memory-leak-in-pgraphics/24500/6 "2020-10-11T22:45:33Z")

</div>

> [@NullPointer](#):
>
> … but I believe that the issue is from drawing a circle to the graphic.

> [@NullPointer](#):
>
> … and I think they are just being cached so quickly that they can’t be GC’ed.

Merely drawing circles to a PGraphics doesn’t cache anything, but rather change the contents of its _pixels[]_.

I’m lost to whatever is causing your sketch to spike its RAM usage.

Aren’t you somehow creating too many temporary objects before drawing those circles?

---

<div class="post-metadata">

**Author:** ![NullPointer](https://avatars.discourse-cdn.com/v4/letter/n/b38774/32.png) [@NullPointer](https://discourse.processing.org/u/NullPointer)\
**Post date:** [October 13, 2020, 3:14pm UTC](https://discourse.processing.org/t/memory-leak-in-pgraphics/24500/7 "2020-10-13T15:14:56Z")

</div>

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/199ccbef81146b101fa610c494e19afa90dadfd5.png)  
This is from the memory profiler I have been using. The RenderThread is the thread class we create to create a graphic for each zoom level (only 6 threads for 6 zoom levels). We only create the graphic once and then just redraw onto the graphic. From what I can see it isn’t other temporary objects and maybe just something in the PGraphic we don’t know about. On top of that, if we only draw to the Pgraphic by updating its pixels[] array we don’t experience any memory issues, but drawing like that is difficult since we would have to manually set the correct pixels into a circle.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 13, 2020, 5:36pm UTC](https://discourse.processing.org/t/memory-leak-in-pgraphics/24500/8 "2020-10-13T17:36:44Z")

</div>

Ugh! All of that just to render a single **circle()** over a PGraphicsJava2D? 😱

I’m afraid there’s no much we can do. 😞

Maybe call **[noSmooth()](https://processing.org/reference/noSmooth_.html)** & **blendMode(REPLACE)** over the PGraphics to turn off most of its effects?

> **[blendMode() \\ Language (API) \\ Processing 3+](https://Processing.org/reference/blendMode_.html)**

---

<div class="post-metadata">

**Author:** ![NullPointer](https://avatars.discourse-cdn.com/v4/letter/n/b38774/32.png) [@NullPointer](https://discourse.processing.org/u/NullPointer)\
**Post date:** [October 13, 2020, 5:59pm UTC](https://discourse.processing.org/t/memory-leak-in-pgraphics/24500/9 "2020-10-13T17:59:00Z")

</div>

Well I guess the issue really comes from the fact that we need to draw around 760,000 circles onto a single PGraphic at the highest zoom layer, and then we thread the other layers so they are drawing the zoomed in layers at the same time… just a whole bunch of bloat ☹  
Thank you for the answers though!

---

<div class="post-metadata">

**Author:** ![NullPointer](https://avatars.discourse-cdn.com/v4/letter/n/b38774/32.png) [@NullPointer](https://discourse.processing.org/u/NullPointer)\
**Post date:** [October 13, 2020, 6:11pm UTC](https://discourse.processing.org/t/memory-leak-in-pgraphics/24500/10 "2020-10-13T18:11:45Z")

</div>

We need transparency so we are already using blendMode(ADD), but making the change to all layers having noSmooth() seemed to actually make a massive difference, as well as I made it so that we only render the current layer instead of all layers. I’m sure that theres still some issues but thank you for the suggestion it really helped!

---

<div class="post-metadata">

**Author:** ![Hyperion65](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hyperion65/32/10569_2.png) [@Hyperion65](https://discourse.processing.org/u/Hyperion65)\
**Post date:** [October 13, 2020, 6:48pm UTC](https://discourse.processing.org/t/memory-leak-in-pgraphics/24500/11 "2020-10-13T18:48:02Z")

</div>

hello, maybe my input is a bit off your request, but I cannot help to wonder…

why do you need a thread in the first place ?  
if you zoom/move around, the setup of your trees will not be changed.  
if you change filter options, obviously you will have to create a new display/arrangment of your trees. that - of course - takes time. and you can only provide an updated image after it has been created…

what would you need to happen in that time ? should the user switch filters another time while you re-caclulate ? it doesn’t make too much sense to me… after selecting a certain configuration, I would supposedly want to see the result.  
in fact, someone clicking buttons 5 times in a row should be prevented from doing so, no ?

when you scroll/ zoom, the typical advantage may be that you do not have to calculate all the trees (which might take long). but in my experience calculations don’t eat up as much performance as actually displaying or loading stuff does.

so when you move the image, is it really necessary to recalculate the whole bunch of trees ?  
or… how long does it take to calculate all trees offscreen ?  
maybe you can keep an image of all the trees that you use for moving, scaling. or use “buckets” of offscreen parts that you update when the user does nothing.

however, if there is a long procedure that simply takes time, the user should be aware to expect that it is not being updated in realtime…

in any case: my own experiences with the phenomenon you describe is actually when I call some procedure a second time BEFORE it has been finished.so imagine that (not knowing your method) that by moving around, you call your thread again and again.  
afaik processing uses a method that you call as a thread, yet every time you start this thread (that is many threads in parallel) they literally access the same method having to wait for each other to use it.

so that all sounds strongly like a number of threads stepping on each others feet, because the second call to a thread starts before the first one has finished.

secondly you might be interested in our last work. you may find it quiet… familiar 😉  
[https://www.black-moon.at](https://www.black-moon.at)

it has only 10k trees at most, although each tree is holding more than a single circle.  
we actually used shapes to create the form of each tree at startup, while filtering controls the positioning of trees but not their form.

still displaying all trees takes a couple of seconds, but imo its more of an display capacity issue.

yet, the only thread we use is while loading the 10k shapes into memory, that’s it.
