处理中的内存泄漏

时间:2014-03-26 23:36:57

标签: java memory memory-leaks processing heap-memory

使用Processing,我正在开发一个项目,可以随机地将txt文件中的视频剪辑和字符串配对。每当我运行程序时,最终它会停止并且我收到一堆相同的错误:

  

JNA:回调org.gstreamer.elements.AppSink $ 3@1bf404f扔了   以下异常:java.lang.OutofMemoryError:Java堆空间

我怀疑我处理Movie对象的创建和取消分配存在内存泄漏问题,但我无法弄清楚错误是什么。 setup()是我首先实例化Movie对象以准备第一次迭代的地方。在我的draw()中,对我来说,看起来我每次重新实例化时都会将Movie对象重置为null,在我看来应该注意内存问题,但似乎并非如此。

有人能提供解决方案吗?在我的偏好中,我的内存增加到256 MB,但我知道增加内存只会延迟不可避免的错误。谢谢!

这是我的代码:

import processing.video.*;

PFont font;

String[] posts; // strings loaded in setup()
String[] videos = {"1a.mov", "2a.mov", "3a.mov", "4a.mov", "5a.mov", "6a.mov",
                    "7a.mov", "8a.mov", "9a.mov"}; // video clips

String post;
Post first; // First post
Post p; // Next iteration of posts

Movie myMovie;
String clip;
int count; // Iteration counter

int a = 0; // image()
float duration = 0; // Movie duration
float time = 0; // Movie time

void setup(){

  size(displayWidth, displayHeight);
  background(0);

  posts = loadStrings("posts.txt"); // load strings from file
  font = loadFont("HelveticaNeue-Bold-48.vlw"); // load font
  post = posts[int(random(posts.length))]; // use random post

  textFont(font); // Set text font
  textSize(50);
  textAlign(CENTER);
  fill(255, 248, 43); // Yellow fill

  if (frame != null){
    frame.setResizable(true); // resizable window
  }  

  /** Random generation of initial clip and post */
  clip = videos[int(random(videos.length))];
  myMovie = new Movie(this, clip);
  makeTint();
  myMovie.play();
  count++;
  first = new Post(post);
  println("Playing clip: " + clip + " w/ post: " + "\"" + post + "\"");
  println("Iteration: " + count + "\n");
}

/** Will generate random clips and posts after initial clip **/
void draw(){

  if (a == 0){
    image(myMovie, 0, 0);
  }

  image(myMovie, 0, 0);

  duration = myMovie.duration();
  time = myMovie.time();

  /** If clip is at end **/
  if ((duration - time) < 0.1){

    first = null; // Remove first post

    /** Reset clip **/
    clip = null;
    myMovie = null;
    clip = videos[int(random(videos.length))];
    myMovie = new Movie(this, clip);
    count++;

    makeTint();
    myMovie.play();

    /** Reset post **/
    p = null;
    post = posts[int(random(posts.length))];

    println("Playing clip: " + clip + " w/ post: " + "\"" + post + "\"");
    println("Post length: " + post.length());
    println("Iteration: " + count + "\n");

  }
    p = new Post(post);
}

/** Method needed to play clips **/
void movieEvent(Movie m){  
  m.read();  
}

/** Class for displaying post **/
class Post{

  /*******************************************\
   * Function: Post Object Constructor
   * Parameter Description:
   *-----------------------------------------
   * t, text to display
  \*******************************************/
  Post(String t){
    text(t, width/2, height - 150);  
  }

}

2 个答案:

答案 0 :(得分:1)

Movie有一个dispose()方法,似乎可以清除所有gstreamer分配。我怀疑这可能是原因。

在代码中尝试在设置myMovie.dispose();之前调用myMovie = null

有时您需要深入了解源代码以查看所有可用内容:https://github.com/processing/processing/blob/master/java/libraries/video/src/processing/video/Movie.java

**同时增加应用程序内存对于一般用途来说是完全合理的。 256MB很小,我的设置为1024MB。但是我在提高之前先解决这个漏洞。

答案 1 :(得分:0)

I'm facing the same problem, and for me at least the disposte() method didn't solve the issue.

By testing and checking the memory used, I saw that the stop() and loop() functions keep increasing the memory.

The solution I found, is to use jump(0) once the video was over instead of loop() function and pause() and jump(0) instead of the stop().

This stopped the memory leak for me.