# OutOfMemory error after loading many gifs

**URL:** https://discourse.processing.org/t/outofmemory-error-after-loading-many-gifs/27156
**Category:** Libraries
**Created:** [January 18, 2021, 8:02am UTC](https://discourse.processing.org/t/outofmemory-error-after-loading-many-gifs/27156 "2021-01-18T08:02:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![AbleYudada](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ableyudada/32/14190_2.png) [@AbleYudada](https://discourse.processing.org/u/AbleYudada)
#### Post date: [January 18, 2021, 8:02am UTC](https://discourse.processing.org/t/outofmemory-error-after-loading-many-gifs/27156/1 "2021-01-18T08:02:18Z")

</div>

```auto

import gifAnimation.*;//

ArrayList<Gif> loopingGifs;//

void setup() {
  //size(512, 414, P3D);
  fullScreen(P3D,2);
  frameRate(60);
  
  loopingGifs = new ArrayList<Gif>();

  }
void draw()
{
    if(t&&frameCount % 30 == 0)
  {
   thread("post");//多线程加载机制，不需要占用主线程时间。
  if(loopingGifs.size()>0&&loopingGifs.get(loopingGifs.size()-1).isLoaded())
  {
    t = false;
  }
  }
}
void post()
{
loopingGifs.add(new Gif(this, "hh.gif"));
}

```

Hello, everyone. This is part of my code. After I loaded multiple gifs and then removed them by loopingGifs.remove(), I found that the memory did not decrease. When I used many gifs, there was an error of no memory.Is there any good way to solve this problem？

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 18, 2021, 9:23am UTC](https://discourse.processing.org/t/outofmemory-error-after-loading-many-gifs/27156/2 "2021-01-18T09:23:47Z")

</div>

try to overwrite them instead of adding and erasing the previous. If that doesn’t work, increase the allocated memory in preferences.

---

<div class="post-metadata">

### Author: ![AbleYudada](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ableyudada/32/14190_2.png) [@AbleYudada](https://discourse.processing.org/u/AbleYudada)
#### Post date: [January 18, 2021, 9:44am UTC](https://discourse.processing.org/t/outofmemory-error-after-loading-many-gifs/27156/3 "2021-01-18T09:44:19Z")

</div>

Thank you for your answer, but the workload of overwrite will be very large, and I need to rewrite most of the code.Increasing the entire memory is probably not a good idea, because I deleted it soon after it was created but the memory is still increasing

---

<div class="post-metadata">

### Author: ![AbleYudada](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ableyudada/32/14190_2.png) [@AbleYudada](https://discourse.processing.org/u/AbleYudada)
#### Post date: [January 18, 2021, 9:44am UTC](https://discourse.processing.org/t/outofmemory-error-after-loading-many-gifs/27156/4 "2021-01-18T09:44:59Z")

</div>

```auto
import gifAnimation.*;
ParticleSystem ps;
ArrayList<Gif> loopingGifs;
//int id=0;
boolean t = false;
void setup() {
  size(1280, 720,P3D);
  imageMode(CENTER);
  ps = new ParticleSystem();
  loopingGifs = new ArrayList<Gif>();
  
}
void draw() {
 
  background(0);

  if(t&&frameCount % 30 == 0)
  {
 thread("post");
  if(loopingGifs.size()>0&&loopingGifs.get(loopingGifs.size()-1).isLoaded())
  {
    t = false;
    //id+=1;
  }
  }
  ps.run();
  String txt_fps = String.format(getClass().getName()+ " [fps %6.2f] [frame %d] ", frameRate, frameCount);
    surface.setTitle(txt_fps);
}
void mouseClicked()
  {
   t = true; 
   ps.addParticle(new PVector(mouseX,mouseY));
  }
void post()
{
loopingGifs.add(new Gif(this, "g.gif"));
}
class Particle {
  PVector location;
  PVector velocity;
  PVector acceleration;
  int id ;
  float lifespan;
  boolean bofang = true;
  Particle(PVector l) {
    // The acceleration
    acceleration = new PVector(0, 0.05);
    // circel's x and y ==> range
   velocity = new PVector(random(-1, 1), random(-2, 0));
    // apawn's position
    location = l.copy();
    // the circle life time
    lifespan = 255.0;
    
  }
  void run() {
    update();
    display();
  }
  void update() {
   // velocity.add(acceleration);
   //location.add(velocity);
    lifespan-=0.2;
  }
 
  boolean isDead() {
    if (lifespan <= 0) {
      return true;
    } else {
      return false;
    }
  }
  void display() {
    // border
    stroke(0, lifespan);
    // border's weight
    strokeWeight(1);
    float r = random(0,255);
    float g = random(0,255);
    float b = random(0,255);
    // random the circle's color
    fill(r,g,b, lifespan);
    // draw circle
    ellipse(location.x, location.y, 3, 3);
     
    if(loopingGifs.size()>0)
    {
       image(loopingGifs.get(id),location.x,location.y);
      if(loopingGifs.get(id).isLoaded())
      {
        println(loopingGifs.size(),loopingGifs.get(id).isLoaded());
   
    if(bofang)
    {
    loopingGifs.get(id).play();
    loopingGifs.get(id).ignoreRepeat();
    
    bofang = false;
    }
          if(!loopingGifs.get(id).isPlaying())
    {
     // lifespan = 0;
    }
   // println(loopingGifs.get(id).isLooping());
      }
    }

   
  }
  
}

class ParticleSystem {
  ArrayList<Particle> particles;
  PVector origin;
 
  ParticleSystem() {
   
    particles = new ArrayList<Particle>();
  }
 
  void addParticle(PVector pos) {
     origin = pos.copy();
    particles.add(new Particle(origin));
  }
 
  void run() {
    for (int i = particles.size()-1; i >= 0; i--) {
      Particle p = particles.get(i);
      p.id = i;
      p.run();
       
      if (p.isDead()) {
        particles.remove(i);
        loopingGifs.get(i).dispose();
        loopingGifs.remove(i);
        
      }
    
    }
  }
}

```

Here is all my code！
