# Out of Memory when creating image using canvas

**URL:** <https://discourse.processing.org/t/out-of-memory-when-creating-image-using-canvas/44296>\
**Category:** Processing\
**Created:** [April 16, 2024, 6:38pm UTC](https://discourse.processing.org/t/out-of-memory-when-creating-image-using-canvas/44296 "2024-04-16T18:38:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TellAJoke](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tellajoke/32/18982_2.png) [@TellAJoke](https://discourse.processing.org/u/TellAJoke)\
**Post date:** [April 16, 2024, 6:38pm UTC](https://discourse.processing.org/t/out-of-memory-when-creating-image-using-canvas/44296/1 "2024-04-16T18:38:16Z")

</div>

Project runs out of memory eventually after running for a while. (RuntimeException: java.lang.OutOfMemoryError: Java heap space)

I have narrowed down the issue to a particular snippet. Created a sketch with it that also runs out of memory:

```auto
void setup(){
  size(1000, 1000, P2D);
}

void draw(){
  PImage img = testImage(width,height);
  image(img, 0, 0);
  img = null;
}

PImage testImage(float _w, float _h){
    int w = floor(_w);
    int h = floor(_h);
    PGraphics canvas = createGraphics(w, h, P2D);

    canvas.beginDraw();

    PImage image = new PImage(w, h, RGB);

    //canvas.shader(shader);
    canvas.image(image, 0, 0);

    canvas.endDraw();

    return canvas;
}

```

Any clue what might be happening?  
Thanks!

---

<div class="post-metadata">

**Author:** ![TellAJoke](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tellajoke/32/18982_2.png) [@TellAJoke](https://discourse.processing.org/u/TellAJoke)\
**Post date:** [April 16, 2024, 8:01pm UTC](https://discourse.processing.org/t/out-of-memory-when-creating-image-using-canvas/44296/2 "2024-04-16T20:01:32Z")

</div>

Managed to find a workaround that doesn’t cause memory leak:

```auto
void setup() {
  size(1000, 1000, P2D);
}

void draw() {
  PGraphics canvas = createGraphics(width, height);
  drawOnCanvas(canvas);
  image(canvas, 0, 0);
}

void drawOnCanvas(PGraphics _canvas) {
  _canvas.beginDraw();

  PImage image = new PImage(_canvas.width, _canvas.height, RGB);

  //_canvas.shader(shader);
  _canvas.image(image, 0, 0);

  image = null;

  _canvas.endDraw();

  _canvas = null;
}

```

But not sure why this works and the previous example does not 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 17, 2024, 8:48am UTC](https://discourse.processing.org/t/out-of-memory-when-creating-image-using-canvas/44296/3 "2024-04-17T08:48:44Z")

</div>

> [@TellAJoke](#):
>
> But not sure why this works and the previous example does not

In the first example the canvas uses P2D (OpenGL) and in the second case it uses JAVA2D (Java AWT). I don’t know if this is the cause but it is the only significant difference that I can see.

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 17, 2024, 5:58pm UTC](https://discourse.processing.org/t/out-of-memory-when-creating-image-using-canvas/44296/4 "2024-04-17T17:58:52Z")

</div>

Interestingly some years ago I had a P3D project that needed thousands of frame buffers and there was a memory leak after a while. I couldn’t solve the issue and I had to port some part of the code to openFrameworks and then I didn’t see the issue.

But @TellAJoke is there a reason why you need to create image and buffer every draw frame? Can’t you initialize it in setup and reuse it?

---

<div class="post-metadata">

**Author:** ![TellAJoke](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tellajoke/32/18982_2.png) [@TellAJoke](https://discourse.processing.org/u/TellAJoke)\
**Post date:** [June 14, 2024, 10:43pm UTC](https://discourse.processing.org/t/out-of-memory-when-creating-image-using-canvas/44296/5 "2024-06-14T22:43:57Z")

</div>

Sorry for the late reply.  
I’m showcasing a series of different shaders at once. The shaders change over time too, so I’m creating new images every frame.
