[英]Memory Leak in Processing
使用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);
}
}
Movie
有一個dispose()
方法,該方法似乎可以清除所有gstreamer分配。 我懷疑這可能是原因。
在您的代碼中嘗試調用: myMovie.dispose();
在設置myMovie = null
之前。
有時您需要深入研究源代碼以查看所有可用的代碼: https : //github.com/processing/processing/blob/master/java/libraries/video/src/processing/video/Movie.java
**對於一般用途,增加應用程序內存也是完全合理的。 256MB很小,我的設置為1024MB。 但我會先解決此泄漏,然后再提出。
我面臨着同樣的問題,至少對我而言, disposte()
方法無法解決問題。
通過測試和檢查所使用的內存,我看到stop()
和loop()
函數不斷增加內存。
我發現的解決方案是在視頻結束后使用jump(0)
而不是loop()
函數,而使用pause()
和jump(0)
而不是stop()
。
這為我阻止了內存泄漏。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.