2012-05-19 56 views
0

這會在運行幾秒鐘後產生OutOfMemory異常。有任何想法嗎?處理圖像時的OutOfMemory

PGraphics img; 

void setup() { 
    size(500, 500); 
    img = createGraphics(width, height, JAVA2D); 

    // this is here just for the testcase because else I get a 
    // NullPointerException too (probably a harmless Processing bug) 
    img.beginDraw(); img.endDraw(); 
} 

void draw() { 
    PGraphics tmpImg = createGraphics(img.width, img.height, JAVA2D); 
    tmpImg.beginDraw(); 
    tmpImg.image(img, 0, 0); 
    tmpImg.endDraw(); 
    tmpImg.dispose(); 
} 
+0

它可能有可能是你的內存泄漏的地方。這可以通過一個好的分析器軟件或者一個非常好的調試器來找到。 – evotopid

+0

你是否真的應該在每次輸入draw時創建一個新的PGraphics? – Mat

回答

2

馬特的權利,你不應該實例化一個新的PGraphics每幀。 你可以簡單地做這樣的事情:

PGraphics img; 

void setup() { 
    size(500, 500); 
    img = createGraphics(width, height, JAVA2D); 

    // this is here just for the testcase because else I get a 
    // NullPointerException too (probably a harmless Processing bug) 
    img.beginDraw(); img.endDraw(); 
} 

void draw() { 
    image(img, 0, 0); 
} 

因爲PGraphics延伸PImage。 通常你會使用basic PImage API,但如果你需要繪製形狀到一個位圖或假的「層」,你可以在PImage結合使用PGraphics,但就是不分配新PGraphics 30至60倍第二。