Gif Animation Library - Still having memory leaks

edited January 2018 in Library Questions

Hi, I already asked this as a new post in an old discussion from two years ago, but as expected nobody answered! ;) So I try my luck again:

I would like to build a wooden frame with a display that shows gifs and jpgs when it is activated by a touch sensor. I am using an Up core board with win10 for this which really is a nice piece of hardware. I am using the gif library from 01010101's github account, which seems to be the newest version (https://github.com/01010101/GifAnimation) and works with processing 3. Unfortunately this library seems to have a memory leak problem as already discussed in here: https://forum.processing.org/two/discussion/12452/animated-gif-slideshow To be honest this discussion did not help me to fix the memory leak problem with this library. I still get the error message after several gifs are shown. This is my test code I am using to explore possible solutions:

import gifAnimation.*;

Gif myGif;
String[] filenames;
String path;

void setup() {

  fullScreen(P2D);
  frameRate(25);
  imageMode(CENTER);
  path = sketchPath();   
  path = path+"\\files\\GifDiv";       
  filenames = listFileNames(path);
  myGif = new Gif(this, path+"\\"+filenames[int(random(0,filenames.length)-1)]);

}

void draw() {

    if (frameCount == 1 ){
      myGif = new Gif(this, path+"\\"+filenames[int(random(0,filenames.length)-1)]);
      myGif.play();
      println(frameCount);
      println("first Play");
      image(myGif, displayWidth/2, displayHeight/2, myGif.width , myGif.height);    
    } else if (frameCount % 40 == 0){
      myGif.stop();
      myGif.dispose();     
      myGif = null;
      background(0);
      println(frameCount);
      println("clear Cache");
  } else if (frameCount % 40 == 1){
      myGif = new Gif(this, path+"\\"+filenames[int(random(0,filenames.length)-1)]);
      myGif.play();
      println(frameCount);
      println("new Play");
      image(myGif, displayWidth/2, displayHeight/2, myGif.width , myGif.height);
  } else{
    println(frameCount+" just Counting Frames");
    image(myGif, displayWidth/2, displayHeight/2, myGif.width , myGif.height);
  }

}

String[] listFileNames(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    String names[] = file.list();
    return names;
  } else {
    // If it's not a directory
    return null;
  }
}

The code might seem odd, but I wanted to test what happens when at one frame in time no gif is shown, disposed and set to null. My hope was that in this moment the memory would be cleared. It wasn't!! The source of this problem might be the libraries dispose() command, which might not totally clear the memory!? But this is the point where it ends for me as I am too stupid to write pure Java-Code, possibly fix this and compile another version of the library. So my hope is to find somebody here who is library coder and might be willing to help!

Best, Tim

Answers

  • Calling new Gif() within draw is as bad as calling loadImage within draw. Load everything in setup and reference it in draw.

  • edited January 2018

    Thank you for the answer. I have about 3 gigs of files that I would like to be randomly loaded. I thought it would be too big to load it all in setup? why is it bad to load it in draw? sure, it might be slower, but wouldn‘t it be the better way to handle larger datasets? in addition i took care not to call new Gif() every frame but only when needed.

Sign In or Register to comment.