我对使用 Java、Kinect (OpenNI) 和 Processing 的实际项目有疑问。
如果我只使用 Processing 和 Java 一切正常,没有什么是绊脚石,我也没有例外。
但是,如果我将处理 Applet 放入 JFrame 中(以解决 Applet 处理方式的一些问题),我会遇到以下问题:
- 每隔 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;
}
}
如何防止 OutOfMemory 异常?
什么可能导致该异常?