Help With Video Code Crashing (ram build up?)

Hello,

I’m pretty new to processing and am making this code for a creative project. Basically the code picks a random spot in a video file and plays for ten seconds. After the ten seconds it picks a new random video file and picks another random spot in the video file and plays for ten seconds etc etc. I want this piece to be able to run for at least a couple hours (as I am installing it in a public space) but after about an hour the code crashes and by that I mean the video just freezes and when I quit from the sketch I get no error message. The video files I’m using are about 3gb each (I cannot make them any smaller). My hypothesis is that objects are pilling up and the ram can’t take it anymore so gives up. I have viewed other forums about object pile up but I’m such a noob I can’t take there solutions and apply them to my code. If there is anyone out there that can give me a solution understandable to a beginning that would be really helpful!

import processing.video.*;
Movie myMovie;


float startClip = 3.0;
float endClip = 10.0;
float durationVid;

String[]vid = {"1.mp4", "2.mp4", "3.mp4", "4.mp4",};

void setup() {
  fullScreen (P2D);
  background (0);
  frameRate(30);
  myMovie = new Movie(this, "1.mp4");
  myMovie.play();
  myMovie.jump(startClip);
  durationVid = myMovie.duration() - endClip - 0.2;
}

void draw() {
  
  noCursor();
  //Tints the video. The fourth parameter (100) changes the opacity. Try lowering the opacity to 10.
imageMode(CENTER);
  image(myMovie, width/2, height/2);
 
  if (myMovie.time() > startClip + endClip) {
    myMovie.stop();
    myMovie = null;
    myMovie = new Movie(this, vid[int(random(0, 3))]);
    myMovie.play();
    startClip = random(durationVid);
  myMovie.jump(startClip);
  println(myMovie.duration());
  println(durationVid);
  }
}

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

As with any other resource, you should not load it again if you have already loaded it. If it is possible to load all videos in setup() and avoid using new Movie() in loop, I would recommend to do that.

Otherwise you could request a clean up of the movie by calling dispose(). This should free the memory which the movie is using.

myMovie.stop();
myMovie.dispose();
myMovie = new Movie(this, vid[int(random(0, 3))]);
myMovie.play();
1 Like

Thank you for the reply! I am adding dispose now to the code and will test it out!

As for to avoiding the new Movie() in loop, I’m not sure how else I would have it pick a new video file randomly every ten seconds. If you have any ideas I’d love to hear them! :slight_smile:

Thank you again for your reply!

Hi! Just added the dispose and am still crashing :confused:

Just load all the videos into an array and then select a random index.

Please post the error message. And try to analyse your sketch with VisualVM to see if dispose() is not freeing the memory or what is causing the memory leak.

You could also increase the heap size within the properties.txt.

run.options.memory.initial=64
run.options.memory.maximum=256
1 Like

Just load all the videos into an array and then select a random index.

Do you think you could link me to an example of something like that? (sorry I’m really new)

I assume you’re using Video library v1? You might try v2. The old GStreamer 0.10 bindings have a number of memory leaks. Mind you, the older gst1-java-core in Video library v2 has at least one memory leak that’s since been fixed too.

The array of Movie is a better idea. Ideally the Video library would allow you to have one Movie object and change the source. This is how PraxisLIVE handles this.

@cansik the heap size isn’t so relevant here as all the native resources are allocated off the Java heap. This also leads to a situation where you can run out of memory without the GC being triggered.

1 Like

That sounds great! I’m wondering how you download that. Through the processing app on my mac and searching the libraries I can’t seem to find v2 only v1.01.

Unfortunately still in beta so only at GitHub Release Video Library Release 3 (version 2.0-beta1) · processing/processing-video · GitHub

Hopefully someone will complete the update during GSoC.