【问题标题】:Memory Leak in Processing处理中的内存泄漏
【发布时间】:2014-03-26 23:36:57
【问题描述】:

使用处理,我正在处理一个随机配对视频剪辑和来自 txt 文件的字符串的项目。每当我运行程序时,它最终都会停止,并且我会收到一堆相同的错误:

JNA:回调 org.gstreamer.elements.AppSink$3@1bf404f 抛出 以下异常:java.lang.OutofMemoryError: Java heap space

我怀疑我在处理 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);  
  }

}

【问题讨论】:

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


    【解决方案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。但我会先修复这个漏洞,然后再提出它。

    【讨论】:

    • 似乎这对我有用!该程序运行时间更长,但我遇到了一个 OutofMemoryError,它告诉我增加我的偏好记忆,所以我认为这样做可以解决问题。我应该如何衡量我需要什么应用程序内存?只是将我的数据文件中的数量相加?
    • 太棒了。使用大量视频,我先将其设置为 512MB,然后看看会发生什么。如果仍然出现 OutofMemoryError,请再次加倍。
    【解决方案2】:

    我也面临同样的问题,至少对我来说disposte() 方法并没有解决问题。

    通过测试和检查使用的内存,我看到stop() 和loop() 函数不断增加内存。

    我找到的解决方案是在视频结束后使用jump(0) 而不是loop() 函数和pause() 和jump(0) 而不是stop()。

    这为我阻止了内存泄漏。

    【讨论】:

      猜你喜欢
      • 2015-06-14
      • 1970-01-01
      • 2020-02-29
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多