我對使用Java,Kinect(OpenNI)和Processing的實際項目存在問題。
如果我只使用Processing和Java,一切正常,沒有什麼是絆腳石,我也沒有例外。
但是,如果我坐牢的一個JFrame的處理小程序(解決一些問題處理的小程序風格)我有以下問題:Kinect + Processing + JFrame java.lang.OutOfMemoryError
- 每3秒Kinect的圖像不久掛起(看起來像Java被清除任何東西了使用垃圾收集器)
20秒後停止的應用的內存,我得到以下錯誤:
Exception in thread "Animation Thread" java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.<init>(Unknown Source) at java.awt.image.Raster.createPackedRaster(Unknown Source) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source) at java.awt.image.BufferedImage.<init>(Unknown Source)
這是我的相關代碼對於可視化:
public boolean drawGrayscaleImage(){
//init PApplet and build JFrame
GrayscalePApplet grayscalePApplet = new GrayscalePApplet();
grayscalePApplet.init();
this.grayscaleJFrame = this.initFrame(grayscalePApplet);
//Set Uplink for PApplet and begin drawing
grayscalePApplet.setGraphicP(this);
return false;
}
這裏從處理PApplet類
public void draw(){
if(graphicP != null){
//creat the relevant image Buffers for java and Processing
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_BYTE_GRAY);
PImage pimage = new PImage(image.getWidth(),image.getHeight(),PConstants.ARGB);
//fill up the databuffer using a converted Kinect Grayscale Image
DataBufferByte dataBuffer = new DataBufferByte(graphicP.getImage(ImageType.GRAYSCALE), this.imageWidth * this.imageHeight);
Raster raster = Raster.createPackedRaster(dataBuffer,imageWidth, imageHeight, 8, null);
image.setData(raster);
//draw image to Processing
image.getRGB(0, 0, pimage.width, pimage.height, pimage.pixels, 0, pimage.width);
pimage.updatePixels();
image(pimage, 0, 0);
// null everything to get Garbagecollection to work (?)
image = null;
pimage = null;
dataBuffer = null;
}
}
我怎樣才能防止內存溢出的異常繪圖功能?
什麼可能導致異常?
增加堆大小http://wiki.eclipse.org/FAQ_How_do_I_increase_the_heap_size_available_to_Eclipse%3F –
我增加HEPS大小( -vmargs -Xms512m -Xmx730m)但它對我的問題沒有影響。 Eclipse會顯示堆空間,但不會溢出。 – Rubberducker
由於JVM的堆內存不足,正在引發異常。 ---由於你的GC正在讓程序掛起,然後內存不足,我懷疑你有內存泄漏問題。 ---在這種情況下,簡單地增加@Bhavik_Shan提示的堆大小將只會掩蓋和延遲您的問題,而不是解決問題。您應該調試您的程序,主要側重於優化內存使用。要做的最重要的事情就是驗證是否所有的臨時內存都被釋放,最好是在不再使用時立即釋放。 – XenoRo