# Sprites animation that use a lot of memory

**URL:** <https://discourse.processing.org/t/sprites-animation-that-use-a-lot-of-memory/10695>\
**Category:** Coding Questions\
**Created:** [April 27, 2019, 6:07pm UTC](https://discourse.processing.org/t/sprites-animation-that-use-a-lot-of-memory/10695 "2019-04-27T18:07:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Summer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/summer/32/5244_2.png) [@Summer](https://discourse.processing.org/u/Summer)\
**Post date:** [April 27, 2019, 6:07pm UTC](https://discourse.processing.org/t/sprites-animation-that-use-a-lot-of-memory/10695/1 "2019-04-27T18:07:34Z")

</div>

I made a program that animate sprites but it use a huge amount of memory (~ 3000 MB). I increased the memorry alowed in preference and the program run but it still laggy. Can someone help me to resolve this problem. Thanks for answering 🙂

```auto
PImage[] walk1 = new PImage[43];
PImage[] attack1 = new PImage[40];
PImage[] idle1 = new PImage[50];
PImage[] walk2 = new PImage[40];
PImage[] attack2 = new PImage[45];
PImage[] idle2 = new PImage[48];
void setup(){
  frameRate(60);
  size(1000,600);
  
  for(int i=0;i<=42;i++){
   walk1[i] = loadImage("Préhistoire/Niv 1/Marche/"+(i+1)+".png");
  }
  for(int i=0;i<=39;i++){
   attack1[i] = loadImage("Préhistoire/Niv 1/Attaque/"+(i+1)+".png");
  }
  for(int i=0;i<=49;i++){
   idle1[i] = loadImage("Préhistoire/Niv 1/Immobile/"+(i+1)+".png");
  }
  
  
  for(int i=0;i<=39;i++){
   walk2[i] = loadImage("Préhistoire/Niv 2/Marche/"+(i+1)+".png");
  }
  for(int i=0;i<=44;i++){
   attack2[i] = loadImage("Préhistoire/Niv 2/Attaque/"+(i+1)+".png");
  }
  for(int i=0;i<=47;i++){
   idle2[i] = loadImage("Préhistoire/Niv 2/Immobile/"+(i+1)+".png");
  }
}

void draw(){
 background(0);
 if(mousePressed){
image(attack1[frameCount%40], 200,200,114,117);
image(attack2[frameCount%45], 500,200,218,134);
 }else if(keyPressed){
  image(walk1[frameCount%43],211,226,69,90); 
  image(walk2[frameCount%40],502,200,180,133); 
 }else{
  image(idle1[frameCount%50],228,228,49,88);
  image(idle2[frameCount%48],502,201,179,132);
 }
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [April 27, 2019, 6:13pm UTC](https://discourse.processing.org/t/sprites-animation-that-use-a-lot-of-memory/10695/2 "2019-04-27T18:13:45Z")

</div>

Hi, can you also link the images that your are using ?

---

<div class="post-metadata">

**Author:** ![Summer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/summer/32/5244_2.png) [@Summer](https://discourse.processing.org/u/Summer)\
**Post date:** [April 27, 2019, 7:29pm UTC](https://discourse.processing.org/t/sprites-animation-that-use-a-lot-of-memory/10695/3 "2019-04-27T19:29:03Z")

</div>

Yes of course but how do I do it ? I’m new in this forum so I don’t know how.

---

<div class="post-metadata">

**Author:** ![Summer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/summer/32/5244_2.png) [@Summer](https://discourse.processing.org/u/Summer)\
**Post date:** [April 27, 2019, 8:42pm UTC](https://discourse.processing.org/t/sprites-animation-that-use-a-lot-of-memory/10695/4 "2019-04-27T20:42:17Z")

</div>

Ok i find a way to give a link of the data folder, there it is :  
[https://drive.google.com/open?id=1epCjqgq2cZ06WntQpM4qlA-ZtJWEO5eH](https://drive.google.com/open?id=1epCjqgq2cZ06WntQpM4qlA-ZtJWEO5eH)

---

<div class="post-metadata">

**Author:** ![dan850](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@dan850](https://discourse.processing.org/u/dan850)\
**Post date:** [April 27, 2019, 8:52pm UTC](https://discourse.processing.org/t/sprites-animation-that-use-a-lot-of-memory/10695/5 "2019-04-27T20:52:10Z")

</div>

@Summer Hi you can also copy and paste your code in the message box with The code formatting button

Here are some other tips 🙂[Guidelines—Tips on Asking Questions](https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147)

---

<div class="post-metadata">

**Author:** ![Summer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/summer/32/5244_2.png) [@Summer](https://discourse.processing.org/u/Summer)\
**Post date:** [April 27, 2019, 9:06pm UTC](https://discourse.processing.org/t/sprites-animation-that-use-a-lot-of-memory/10695/6 "2019-04-27T21:06:59Z")

</div>

Ok thanks for the tip 🙂  
Like this the code will be easier for people to understand it :

```auto
PImage[] walk1 = new PImage[43];
PImage[] attack1 = new PImage[40];
PImage[] idle1 = new PImage[50];
PImage[] walk2 = new PImage[40];
PImage[] attack2 = new PImage[45];
PImage[] idle2 = new PImage[48];
void setup() {
  frameRate(60);
  size(1000, 600);

  for (int i=0; i<=42; i++) {
    walk1[i] = loadImage("Préhistoire/Niv 1/Marche/"+(i+1)+".png");
  }
  for (int i=0; i<=39; i++) {
    attack1[i] = loadImage("Préhistoire/Niv 1/Attaque/"+(i+1)+".png");
  }
  for (int i=0; i<=49; i++) {
    idle1[i] = loadImage("Préhistoire/Niv 1/Immobile/"+(i+1)+".png");
  }

  for (int i=0; i<=39; i++) {
    walk2[i] = loadImage("Préhistoire/Niv 2/Marche/"+(i+1)+".png");
  }
  for (int i=0; i<=44; i++) {
    attack2[i] = loadImage("Préhistoire/Niv 2/Attaque/"+(i+1)+".png");
  }
  for (int i=0; i<=47; i++) {
    idle2[i] = loadImage("Préhistoire/Niv 2/Immobile/"+(i+1)+".png");
  }
}

void draw() {
  background(0);
  if (mousePressed) {
    image(attack1[frameCount%40], 200, 200, 114, 117);
    image(attack2[frameCount%45], 500, 200, 218, 134);
  } else if (keyPressed) {
    image(walk1[frameCount%43], 211, 226, 69, 90); 
    image(walk2[frameCount%40], 502, 200, 180, 133);
  } else {
    image(idle1[frameCount%50], 228, 228, 49, 88);
    image(idle2[frameCount%48], 502, 201, 179, 132);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Summer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/summer/32/5244_2.png) [@Summer](https://discourse.processing.org/u/Summer)\
**Post date:** [April 27, 2019, 9:09pm UTC](https://discourse.processing.org/t/sprites-animation-that-use-a-lot-of-memory/10695/7 "2019-04-27T21:09:03Z")

</div>

And there is the data folder with the images if needed :  
[https://drive.google.com/open?id=1epCjqgq2cZ06WntQpM4qlA-ZtJWEO5eH](https://drive.google.com/open?id=1epCjqgq2cZ06WntQpM4qlA-ZtJWEO5eH)

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [April 28, 2019, 3:52pm UTC](https://discourse.processing.org/t/sprites-animation-that-use-a-lot-of-memory/10695/8 "2019-04-28T15:52:21Z")

</div>

Hi,

I downloaded your files and it is almost 700Mo ! Plus your are loading everything at the same time so it’s really heavy on the computer and the ram.

You need to reduce the size of your files. Here are some ideas:

- Lower the number of frame per animation (I can see that sometime you have around a hundred frame for just 1 move. I think you can get a pretty decent look with only 12.
- Lower the size of your images. Some of them are more than 1600x1600 for just a character. I doubt that it will be it’s final size on screen. So no need to make it that big.
- Save as jpg can also help to save some space but sometimes png is need to get nice results.

In addition of that you can build a resources manager. I presume that you won’t need all the sprites all the time so you can load only the one you will be using.

---

<div class="post-metadata">

**Author:** ![Summer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/summer/32/5244_2.png) [@Summer](https://discourse.processing.org/u/Summer)\
**Post date:** [April 28, 2019, 3:58pm UTC](https://discourse.processing.org/t/sprites-animation-that-use-a-lot-of-memory/10695/9 "2019-04-28T15:58:03Z")

</div>

Ok thanks for the answere I’ll try these solutions 😉

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 1, 2019, 5:35am UTC](https://discourse.processing.org/t/sprites-animation-that-use-a-lot-of-memory/10695/10 "2019-05-01T05:35:01Z")

</div>

In addition, there is also a Sprites library for Processing.
