2011-04-27 77 views
0

我正在使用Processing。這裏是我的草圖的全部:處理:內存不足錯誤

import guicomponents.*; 

PImage backgroundImage; 

void setup() {  
    size(911, 715); 
    backgroundImage = loadImage("Floorplan.png"); 
} 
void draw() { 
    background(backgroundImage); 
    GImageButton[] buttons = { 
    new GImageButton(this, null, "wm.png", 1, 10, 10), 
    new GImageButton(this, null, "wm.png", 1, 10, 10), 
    new GImageButton(this, null, "wm.png", 1, 10, 10), 
    new GImageButton(this, null, "wm.png", 1, 10, 10), 
    new GImageButton(this, null, "wm.png", 1, 10, 10), 
    new GImageButton(this, null, "wm.png", 1, 10, 10), 
    new GImageButton(this, null, "wm.png", 1, 10, 10), 
    new GImageButton(this, null, "wm.png", 1, 10, 10), 
    new GImageButton(this, null, "wm.png", 1, 10, 10), 
    new GImageButton(this, null, "wm.png", 1, 10, 10), 
    new GImageButton(this, null, "wm.png", 1, 10, 10), 
    new GImageButton(this, null, "wm.png", 1, 10, 10), 
    new GImageButton(this, null, "wm.png", 1, 10, 10) 
    }; 
} 

(這只是爲了說明我有這個問題的演示。)如果運行足夠長的時間,一個OutOfMemoryError將生成:

An OutOfMemoryError means that your code is either using up too much memory 
because of a bug (e.g. creating an array that's too large, or unintentionally 
loading thousands of images), or that your sketch may need more memory to run. 
If your sketch uses a lot of memory (for instance if it loads a lot of data files) 
you can increase the memory available to your sketch using the Preferences window. 
Exception in thread "Image Fetcher 2" java.lang.OutOfMemoryError: Java heap space 
An OutOfMemoryError means that your code is either using up too much memory 
because of a bug (e.g. creating an array that's too large, or unintentionally 
loading thousands of images), or that your sketch may need more memory to run. 
If your sketch uses a lot of memory (for instance if it loads a lot of data files) 
you can increase the memory available to your sketch using the Preferences window. 
    at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41) 
    at java.awt.image.Raster.createPackedRaster(Raster.java:458) 
    at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015) 
    at sun.awt.image.ImageRepresentation.createBufferedImage(ImageRepresentation.java:230) 
    at sun.awt.image.ImageRepresentation.setPixels(ImageRepresentation.java:528) 
    at sun.awt.image.ImageDecoder.setPixels(ImageDecoder.java:120) 
    at sun.awt.image.PNGImageDecoder.sendPixels(PNGImageDecoder.java:531) 
    at sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:452) 
    at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246) 
    at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172) 
    at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136) 
Exception in thread "Animation Thread" java.lang.OutOfMemoryError: Java heap space 
    at processing.core.PImage.<init>(Unknown Source) 
    at processing.core.PApplet.loadImageMT(Unknown Source) 
    at processing.core.PApplet.loadImage(Unknown Source) 
    at processing.core.PApplet.loadImage(Unknown Source) 
    at guicomponents.GImageButton.getImages(GImageButton.java:136) 
    at guicomponents.GImageButton.<init>(GImageButton.java:100) 
    at gimage_demo.draw(gimage_demo.java:35) 
    at processing.core.PApplet.handleDraw(Unknown Source) 
    at processing.core.PApplet.run(Unknown Source) 
    at java.lang.Thread.run(Thread.java:619) 

爲什麼會發生這種情況?當數組超出範圍時,存儲器是否會收集垃圾回收?draw()的末尾?

我試圖做一個按鈕抽搐時遇到了這個問題。我找不到改變位置的方法,所以我只是在我想要的新位置創建一個新的位置。有一個更好的方法嗎?

回答

1

爲什麼會發生這種情況?當數組在draw()的末尾超出作用域時,內存是否會被垃圾回收?

唯一可以保證的是buttons數組在超出作用域時有資格進行垃圾回收。對於GImageButton對象及其成員,它取決於構造函數具有哪些副作用。

確保構造函數不(在你的情況this)的第一個參數「註冊」本身,而沒有緩存回事,或者說,GImageButton不會泄漏這是從構造方法中this參考,等等。

1

查看代碼(http://code.google.com/p/gui4processing/source/browse/trunk/GUI4Processing/src/guicomponents/G4P.java?r=331),顯示所有GImageButton實例存儲在一個靜態的HashMap中。我看不到一種方式來處理它們(儘管我沒有看過那麼難)。因此他們不會有資格進行垃圾回收。

我希望答案是(例如,在setup)一旦創建按鈕,然後在draw使用它們(除非你能找到一個dispose()方法是等效的)。