# Calling createGraphics() many times causing memory leak

**URL:** <https://discourse.processing.org/t/calling-creategraphics-many-times-causing-memory-leak/22388>\
**Category:** p5.js\
**Created:** [July 4, 2020, 5:22pm UTC](https://discourse.processing.org/t/calling-creategraphics-many-times-causing-memory-leak/22388 "2020-07-04T17:22:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![benp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/benp/32/9839_2.png) [@benp](https://discourse.processing.org/u/benp)\
**Post date:** [July 4, 2020, 5:22pm UTC](https://discourse.processing.org/t/calling-creategraphics-many-times-causing-memory-leak/22388/1 "2020-07-04T17:22:48Z")

</div>

I have [a sketch](https://github.com/benparsons/p5sketches/blob/master/2020-07-04-grunberger1/sketch5.js) I’ve been working on, and I found that calling `createGraphics()` repeatedly causes memory to not be freed. Representative example:

```auto
var size = 600;

function setup() {
  createCanvas(size, size);
  stroke(0);
  strokeWeight(10);
}

function draw() {
  let pg = createGraphics(size, size);
  pg.rect(size/4, size/4, size/2, size/2);
  image(pg, 0, 0, 600, 600);
}

```

How can I delete the `pg` instance between draws?

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [July 5, 2020, 8:31am UTC](https://discourse.processing.org/t/calling-creategraphics-many-times-causing-memory-leak/22388/2 "2020-07-05T08:31:48Z")

</div>

I Javascript garbage collection takes care of releasing unused memory, but it can take a while. Garbage collection in simplest cases cleans objects that have no references any more, but there are other strategies too. For more information [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory\_Management](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management) or [https://blog.sessionstack.com/how-javascript-works-memory-management-how-to-handle-4-common-memory-leaks-3f28b94cfbec](https://blog.sessionstack.com/how-javascript-works-memory-management-how-to-handle-4-common-memory-leaks-3f28b94cfbec)

You can try to set pg variable to null to make sure that there are no references to created graphics object, but in the end it’s up to garbage collection to free the memory. Most likely it depends on browser and their javascript implementation how, when and how it happens. After all that script creates 60 graphics objects in a second so some of them will be in the existence at the same time.

---

<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:** [July 6, 2020, 2:40am UTC](https://discourse.processing.org/t/calling-creategraphics-many-times-causing-memory-leak/22388/3 "2020-07-06T02:40:51Z")

</div>

I’ve noticed this in regular processing too. The temporary PImages would use the same var name and therefore shouldn’t need to use extra memory yet on every loop memory would go up constantly until memory leak error.

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [July 6, 2020, 4:21am UTC](https://discourse.processing.org/t/calling-creategraphics-many-times-causing-memory-leak/22388/4 "2020-07-06T04:21:51Z")

</div>

Java uses garbage collection too to clear unused memory. Variable is just a reference to object, that has memory allocated to it. Clearing variable or putting another object to it does not free the memory. It happens later in the background. If you consume memory faster than garbage collection can free it, you’ll run out of memory.

---

<div class="post-metadata">

**Author:** ![benp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/benp/32/9839_2.png) [@benp](https://discourse.processing.org/u/benp)\
**Post date:** [July 14, 2020, 12:59pm UTC](https://discourse.processing.org/t/calling-creategraphics-many-times-causing-memory-leak/22388/5 "2020-07-14T12:59:43Z")

</div>

Helpful, thanks @SomeOne.
