# 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:** 1\
**Showing post:** 4

<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!

---

_[View the full topic](https://discourse.processing.org/t/memory-leak-in-pgraphics/24500)._
