Help With Video Code Crashing (ram build up?)


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!

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);
  myMovie = new Movie(this, "1.mp4");;
  durationVid = myMovie.duration() - endClip - 0.2;

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

void movieEvent(Movie m) {;

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 = new Movie(this, vid[int(random(0, 3))]);;
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.

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.