# Help With Video Code Crashing (ram build up?)

**URL:** https://discourse.processing.org/t/help-with-video-code-crashing-ram-build-up/9688
**Category:** Libraries
**Created:** [March 27, 2019, 5:42pm UTC](https://discourse.processing.org/t/help-with-video-code-crashing-ram-build-up/9688 "2019-03-27T17:42:17Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![EvanA](https://avatars.discourse-cdn.com/v4/letter/e/57b2e6/32.png) [@EvanA](https://discourse.processing.org/u/EvanA)
#### Post date: [March 27, 2019, 5:42pm UTC](https://discourse.processing.org/t/help-with-video-code-crashing-ram-build-up/9688/1 "2019-03-27T17:42:17Z")

</div>

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!

```auto
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();
}

```

---

<div class="post-metadata">

### Author: ![cansik](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/cansik/32/8815_2.png) [@cansik](https://discourse.processing.org/u/cansik)
#### Post date: [March 27, 2019, 5:49pm UTC](https://discourse.processing.org/t/help-with-video-code-crashing-ram-build-up/9688/2 "2019-03-27T17:49:50Z")

</div>

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.

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

```

---

<div class="post-metadata">

### Author: ![EvanA](https://avatars.discourse-cdn.com/v4/letter/e/57b2e6/32.png) [@EvanA](https://discourse.processing.org/u/EvanA)
#### Post date: [March 27, 2019, 6:24pm UTC](https://discourse.processing.org/t/help-with-video-code-crashing-ram-build-up/9688/3 "2019-03-27T18:24:35Z")

</div>

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! 🙂

Thank you again for your reply!

---

<div class="post-metadata">

### Author: ![EvanA](https://avatars.discourse-cdn.com/v4/letter/e/57b2e6/32.png) [@EvanA](https://discourse.processing.org/u/EvanA)
#### Post date: [March 27, 2019, 6:29pm UTC](https://discourse.processing.org/t/help-with-video-code-crashing-ram-build-up/9688/4 "2019-03-27T18:29:57Z")

</div>

Hi! Just added the dispose and am still crashing 😕

---

<div class="post-metadata">

### Author: ![cansik](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/cansik/32/8815_2.png) [@cansik](https://discourse.processing.org/u/cansik)
#### Post date: [March 27, 2019, 6:36pm UTC](https://discourse.processing.org/t/help-with-video-code-crashing-ram-build-up/9688/5 "2019-03-27T18:36:16Z")

</div>

> [@EvanA](#):
>
> 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.

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

> [@EvanA](#):
>
> Hi! Just added the dispose and am still crashing 😕

Please post the error message. And try to analyse your sketch with [VisualVM](https://visualvm.github.io/) 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](https://processing.org/reference/environment/#Preferences).

```ini
run.options.memory.initial=64
run.options.memory.maximum=256

```

---

<div class="post-metadata">

### Author: ![EvanA](https://avatars.discourse-cdn.com/v4/letter/e/57b2e6/32.png) [@EvanA](https://discourse.processing.org/u/EvanA)
#### Post date: [March 27, 2019, 6:56pm UTC](https://discourse.processing.org/t/help-with-video-code-crashing-ram-build-up/9688/6 "2019-03-27T18:56:17Z")

</div>

> 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)

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [March 27, 2019, 7:14pm UTC](https://discourse.processing.org/t/help-with-video-code-crashing-ram-build-up/9688/7 "2019-03-27T19:14:35Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![EvanA](https://avatars.discourse-cdn.com/v4/letter/e/57b2e6/32.png) [@EvanA](https://discourse.processing.org/u/EvanA)
#### Post date: [March 27, 2019, 7:19pm UTC](https://discourse.processing.org/t/help-with-video-code-crashing-ram-build-up/9688/8 "2019-03-27T19:19:56Z")

</div>

> [@neilcsmith](#):
>
> Video library v2

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.

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [March 27, 2019, 7:26pm UTC](https://discourse.processing.org/t/help-with-video-code-crashing-ram-build-up/9688/9 "2019-03-27T19:26:59Z")

</div>

> [@EvanA](#):
>
> That sounds great! I’m wondering how you download that

Unfortunately still in beta so only at GitHub [https://github.com/processing/processing-video/releases/tag/r3-v2.0-beta1](https://github.com/processing/processing-video/releases/tag/r3-v2.0-beta1)

Hopefully someone will complete the update during GSoC.
