# Memory usage increases as images are loaded. Help me

**URL:** <https://discourse.processing.org/t/memory-usage-increases-as-images-are-loaded-help-me/39450>\
**Category:** Coding Questions\
**Created:** [October 26, 2022, 9:47pm UTC](https://discourse.processing.org/t/memory-usage-increases-as-images-are-loaded-help-me/39450 "2022-10-26T21:47:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [October 26, 2022, 9:47pm UTC](https://discourse.processing.org/t/memory-usage-increases-as-images-are-loaded-help-me/39450/1 "2022-10-26T21:47:47Z")

</div>

```auto
PImage fig_image;
long TIMER_RE = 0;

void setup(){
  size(1000,1000);
  fig_image = loadImage("fig_image.jpg"); 
  
}

void draw(){
  if(TIMER_RE < millis()){
     TIMER_RE = millis()+1000;
     fig_image = loadImage("fig_image.jpg");     
  } 
  image(fig_image,0,0,width,height);
}

```

There is only one image variable, so why does the PC’s memory keep increasing? Any solution?

![a1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/6/e69cf18660ddf9b1d49d0c15b6cfbc24a7283742.jpeg)

![a2](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/1/915500bf4902c2db555b178b895977d5eadfb0c2.jpeg)

Images are often loaded. However, I don’t know why the ‘PC memory’ keeps increasing even though there is only one image stored in the variable.

Any solution?

---

<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 26, 2022, 10:16pm UTC](https://discourse.processing.org/t/memory-usage-increases-as-images-are-loaded-help-me/39450/2 "2022-10-26T22:16:39Z")

</div>

[**removecache()**](https://forum.processing.org/two/discussions/tagged%3FTag=removecache().html)

> [@Memory leak in PGraphics](https://discourse.processing.org/t/memory-leak-in-pgraphics/24500/4):
>
> 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: [background()](https://processing.org/reference/background_.html)[set()](https://processing.org/reference/set_.html)[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[…

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [October 26, 2022, 10:17pm UTC](https://discourse.processing.org/t/memory-usage-increases-as-images-are-loaded-help-me/39450/3 "2022-10-26T22:17:33Z")

</div>

Hi

Try to make timer like this

```auto
int start;
int end ;
void setup() {
  size(400,400);
  
}

void draw() {
  //background(0);
  start = millis() - end;
  if (start >= 500) {
    end = millis();
  rect(10, 10, 180, 180);
}
}

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [October 26, 2022, 10:28pm UTC](https://discourse.processing.org/t/memory-usage-increases-as-images-are-loaded-help-me/39450/4 "2022-10-26T22:28:27Z")

</div>

Hi

I think you keep loop load image in void draw() thats why your memory low

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [October 26, 2022, 10:41pm UTC](https://discourse.processing.org/t/memory-usage-increases-as-images-are-loaded-help-me/39450/5 "2022-10-26T22:41:37Z")

</div>

@jafal  
Thank you for answer.  
However, that’s not what I want.  
This is the implementation of the above source code for testing.

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [October 26, 2022, 10:41pm UTC](https://discourse.processing.org/t/memory-usage-increases-as-images-are-loaded-help-me/39450/6 "2022-10-26T22:41:52Z")

</div>

@GoToLoop  
Thank you for answer.  
There is a source code that can solve it. The problem is the delay.

there is a problem with lag.

```auto
void draw() {
  if(TIMER_RE < millis()){
     TIMER_RE = millis()+1000;     
     fig_image = null; System.gc();
     fig_image = loadImage("fig_image.jpg");   
  } 
  image(fig_image, 0, 0, width, height);
}

```

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [October 26, 2022, 11:01pm UTC](https://discourse.processing.org/t/memory-usage-increases-as-images-are-loaded-help-me/39450/7 "2022-10-26T23:01:31Z")

</div>

@GoToLoop

I am making a ‘viewer program’ that allows you to zoom in and out through an image.  
If you use ‘System.gc();’ like the video, a delay problem occurs.

If so, do you have any solution?

[![](https://img.youtube.com/vi/ra17P1y82_w/maxresdefault.jpg "program cash memory test") ](https://www.youtube.com/watch?v=ra17P1y82_w)

As seen in the video above,  
‘System.gc();’ If you use , there will be lag.  
The current implementation was made using the ‘get’ function in ‘image’.  
If you have any solution, please let me know.
