Help with Garbage Collection

edited December 2013 in Arduino

Hello everyone, I finished working on an installation that went successfully except for one small detail I couldn't figure out. I created a projection onto a coke machine which corresponded with 9 FSR sensors I placed over the machines buttons. Each button triggered a different video on the projection. The only problem I had was after pressing the buttons a few times the projection started lagging and I had to re-run the sketch to get rid of it. Does anyone know a garbage collection code for Processing to solve this problem so my variables don't stay stored in memory and slow everything down? I tried one at the bottom but it did not work. Please help if you can and your welcome to use my code if you have any similar projects. Thanks!

import cc.arduino.*;
import processing.serial.*;
import processing.video.*;

Arduino arduino;
Movie movie1;
Movie movie2;
Movie movie3;
Movie movie4;
Movie movie5;
Movie movie6;
Movie movie7;
Movie movie8;
Movie movie9;
Movie movie10;


int fsrval1 = 0;      // Set FSR value
int fsrpin1 = 0;
int fsrval2 = 0;      // Set FSR value
int fsrpin2 = 1;
int fsrval3 = 0;      // Set FSR value
int fsrpin3 = 2;      // Set FSR pin
int fsrval4 = 0;      // Set FSR value
int fsrpin4 = 3;
int fsrval5 = 0;      // Set FSR value
int fsrpin5 = 4;
int fsrval6 = 0;      // Set FSR value
int fsrpin6 = 5;      // Set FSR pin
int fsrval7 = 0;      // Set FSR value
int fsrpin7 = 6;
int fsrval8 = 0;      // Set FSR value
int fsrpin8 = 7;
int fsrval9 = 0;      // Set FSR value
int fsrpin9 = 8;      // Set FSR pin

int WhichMovie = 0;
boolean isPlaying = false;
boolean sketchFullScreen() {

    return true;
}



void setup() 
{
  size(displayWidth, displayHeight);
  frameRate(24);
  background(0);

  println(Arduino.list());

  arduino = new Arduino(this, Arduino.list()[5], 57600);
  arduino.pinMode(fsrpin1, Arduino.INPUT);


  movie1 = new Movie(this, "pepsi.mp4");
  movie2 = new Movie(this, "dietpepsi.mp4");
  movie3 = new Movie(this, "sierra_mist.mp4");
  movie4 = new Movie(this, "dietdew.mp4");
  movie5 = new Movie(this, "lipton brisk.mp4");
  movie6 = new Movie(this, "dew.mp4");
  movie7 = new Movie(this, "crush1.mp4");
  movie8 = new Movie(this, "water.mp4");
  movie9= new Movie(this, "fruit punch.mp4");
  movie10 = new Movie(this, "loop.mp4");
}

void movieEvent(Movie m) {
  m.read();
}


void draw()
{
  fsrval1 = arduino.analogRead(fsrpin1);
  println(fsrval1);
  fsrval2 = arduino.analogRead(fsrpin2);
  println(fsrval2);
  fsrval3 = arduino.analogRead(fsrpin3);
  println(fsrval3);
  fsrval4 = arduino.analogRead(fsrpin4);
  println(fsrval4);
  fsrval5 = arduino.analogRead(fsrpin5);
  println(fsrval5);
  fsrval6 = arduino.analogRead(fsrpin6);
  println(fsrval6);
  fsrval7 = arduino.analogRead(fsrpin7);
  println(fsrval7);
  fsrval8 = arduino.analogRead(fsrpin8);
  println(fsrval8);
  fsrval9 = arduino.analogRead(fsrpin9);
  println(fsrval9);

  background(255);             // Set background to white
  if (fsrval1 > 100) {

    WhichMovie = 1;

  }  else if (fsrval2 > 100 ){
    WhichMovie = 2;

  }  else if (fsrval3 > 100 ){
    WhichMovie = 3;

  }  else if (fsrval4 > 100 ){
    WhichMovie = 4;

  }  else if (fsrval5 > 100 ){
    WhichMovie = 5;

  }  else if (fsrval6 > 100 ){
    WhichMovie = 6;

  }  else if (fsrval7 > 100 ){
    WhichMovie = 7;

  }  else if (fsrval8 > 100 ){
    WhichMovie = 8;

}  else if (fsrval9 > 100 ){
    WhichMovie = 9;

  } else {

  }



   if ( WhichMovie == 1 ) {              // If the fsr value is > 200,
    image(movie1, 0, 0, width, height);
      movie1.loop();      // play movie
      //isPlaying = true;
      float mt = movie1.time();
      float md = movie1.duration();
      if(mt >= md -1){
      WhichMovie = 0;

      } 


  } else  if ( WhichMovie == 2  ){    // If the serial value is not 0,
   image(movie2, 0, 0, width, height);
      movie2.loop();
      //isPlaying = true;
      float mt = movie2.time();
      float md = movie2.duration();
      if(mt >= md -1) {

        WhichMovie = 0;
      }

  } else  if ( WhichMovie == 3  ){    // If the serial value is not 0,
   image(movie3, 0, 0, width, height);
      movie3.loop();
      //isPlaying = true;
      float mt = movie3.time();
      float md = movie3.duration();
      if(mt >= md -1) {

        WhichMovie = 0;
      }

  } else  if ( WhichMovie == 4  ){    // If the serial value is not 0,
   image(movie4, 0, 0, width, height);
      movie4.loop();
      //isPlaying = true;
      float mt = movie4.time();
      float md = movie4.duration();
      if(mt >= md -1) {

        WhichMovie = 0;
      }

  } else  if ( WhichMovie == 5  ){    // If the serial value is not 0,
   image(movie5, 0, 0, width, height);
      movie5.loop();
      //isPlaying = true;
      float mt = movie5.time();
      float md = movie5.duration();
      if(mt >= md -1) {

        WhichMovie = 0;
      }

       } else  if ( WhichMovie == 6  ){    // If the serial value is not 0,
   image(movie6, 0, 0, width, height);
      movie6.loop();
      //isPlaying = true;
      float mt = movie6.time();
      float md = movie6.duration();
      if(mt >= md -1) {

        WhichMovie = 0;
      }

  } else  if ( WhichMovie == 7  ){    // If the serial value is not 0,
   image(movie7, 0, 0, width, height);
      movie7.loop();
      //isPlaying = true;
      float mt = movie7.time();
      float md = movie7.duration();
      if(mt >= md -1) {

        WhichMovie = 0;
      }

  } else  if ( WhichMovie == 8  ){    // If the serial value is not 0,
   image(movie8, 0, 0, width, height);
      movie8.loop();
      //isPlaying = true;
      float mt = movie8.time();
      float md = movie8.duration();
      if(mt >= md -1) {

        WhichMovie = 0;
      }

  } else  if ( WhichMovie == 9  ){    // If the serial value is not 0,
   image(movie9, 0, 0, width, height);
      movie9.loop();
      //isPlaying = true;
      float mt = movie9.time();
      float md = movie9.duration();
      if(mt >= md -1) {

        WhichMovie = 0;
      }

  // set fill to light gray
  } else if (WhichMovie == 0){

    image(movie10, 0, 0, width, height);
      movie10.loop();
      //movie10.jump(0.1);
}
 System.gc(); 
}

Answers

  • edited December 2013

    As far as I see, you only instantiate new Movie objects within setup().
    I believe your problem got nothing to do w/ System.gc().
    Rather b/c you end up loop() all Movie objects w/o pausing the others!? @-)

  • This code looks like a strong argument for using arrays. Please consider reading From Several Variables to Arrays when you get the opportunity as it will likely make your coding much simpler.

  • System.gc() won't do you any good if all of your variables are still in scope. In fact, you shouldn't rely on System.gc() for much of anything, as the best case scenario is a "suggestion" to do a garbage collection.

    Do you really need to keep every movie in memory the whole time? Why not just load the movie when the button is pressed, play it, and then close it when you're done?

  • Could also be connected to Processing serial issues, e.g. this one.

  • edited December 2013

    Do you really need to keep every movie in memory the whole time?

    My guess is that if those videos aren't so big and Processing got reasonable RAM available for itself,
    loading them all and keep them all in RAM isn't an issue!

    But having couple of them in loop() at the same time certainly is! #-o
    And I don't see a pause or stop code anywhere! @-)

  • Thanks for all the tips guys. I'll look into all your suggestions and see if any of them solve my problem.

    But having couple of them in loop() at the same time certainly is! And I don't see a pause or stop code anywhere!

    The reason I changed them to loop() instead of play() is that they would just play to the end and not restart and I'm not too sure how to do a reset on a video. So I used the time and duration functions to return the values of the time and duration that way it would jump back to my actual loop video that we had looping while no buttons were being pressed.

Sign In or Register to comment.