I have not used the gif animation library before but the problem is almost certainly more generic to Java.
When you do this
myGIf = new Gif(this, fileNames[firstFile]);
the sketch (Java) will reserve memory for the PImage array and the PImages and myGif will be a
reference to where that memory is located in RAM.
When later you do this
myGIf = new Gif(this, fileNames[secondFile]);
you are loading a new gif animation and the reference myGif is changed but the memory used by the first gif is still allocated until it is cleared by the Java garbage collector.
If you are using these statements in the
draw method (or a method called from
draw) then this is repeated every frame - about 60 times a second giving lttle time for the garbage collector to clear out the memory used by old animations.
It would be better to load the animations once in setup and then simply switch between them in draw.
e.g.
- Gif[] gifs = new Gif[2]; // space for 2 animations
- Gif myGif;
- void setup(){
- size(...);
- gifs[0] = new Gif(this, fileNames[firstFile]);
- gifs[1] = new Gif(this, fileNames[secondFile]);
- myGif = gifs[0]; // to switch to the next one use myGif = gifs[1];
- }
- void draw(){
- // do want you need to do to display myGif
- }