Processing 操作图像时的OutOfMemory

Processing 操作图像时的OutOfMemory,processing,out-of-memory,Processing,Out Of Memory,这会在运行几秒钟后产生OutOfMemory异常。有什么想法吗 PGraphics-img; 无效设置(){ 大小(500500); img=createGraphics(宽度、高度、JAVA2D); //这只是为了测试用例,否则我会得到一个 //NullPointerException(可能是一个无害的处理错误) img.beginDraw();img.endDraw(); } 作废提款(){ PGraphics tmpImg=createGraphics(img.width、img.heig

这会在运行几秒钟后产生OutOfMemory异常。有什么想法吗

PGraphics-img;
无效设置(){
大小(500500);
img=createGraphics(宽度、高度、JAVA2D);
//这只是为了测试用例,否则我会得到一个
//NullPointerException(可能是一个无害的处理错误)
img.beginDraw();img.endDraw();
}
作废提款(){
PGraphics tmpImg=createGraphics(img.width、img.height、JAVA2D);
tmpImg.beginDraw();
tmpImg.image(img,0,0);
tmpImg.endDraw();
tmpImg.dispose();
}

没错,你不应该每帧实例化一个新的图形。 您可以简单地执行以下操作:

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结合使用,但不要每秒分配30到60次新的PGraphics。

您可能在某个地方发生内存泄漏。这可以通过一个好的分析器软件或一个非常好的调试器找到。你真的应该在每次输入draw?img.Dispose?时创建一个新的PGraphics吗?。。。