Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 处理:内存不足错误_Java_User Interface_Processing_Out Of Memory - Fatal编程技术网

Java 处理:内存不足错误

Java 处理:内存不足错误,java,user-interface,processing,out-of-memory,Java,User Interface,Processing,Out Of Memory,我正在使用。以下是我的整个草图: import guicomponents.*; PImage backgroundImage; void setup() { size(911, 715); backgroundImage = loadImage("Floorplan.png"); } void draw() { background(backgroundImage); GImageButton[] buttons = { new GImageButto

我正在使用。以下是我的整个草图:

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)
OutOfMemoryError表示您的代码占用了太多内存
由于错误(例如,创建的阵列太大或无意中
加载数千个图像),或者您的草图可能需要更多内存才能运行。
如果草图使用了大量内存(例如,如果它加载了大量数据文件)
可以使用“首选项”窗口增加草图的可用内存。
线程“Image Fetcher 2”java.lang.OutOfMemoryError中出现异常:java堆空间
OutOfMemoryError意味着您的代码占用了太多内存
由于错误(例如,创建的阵列太大或无意中
加载数千个图像),或者您的草图可能需要更多内存才能运行。
如果草图使用了大量内存(例如,如果它加载了大量数据文件)
可以使用“首选项”窗口增加草图的可用内存。
位于java.awt.image.DataBufferInt.(DataBufferInt.java:41)
位于java.awt.image.Raster.createPackedRaster(Raster.java:458)
位于java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015)
在sun.awt.image.ImageRepresentation.createBuffereImage(ImageRepresentation.java:230)中
位于sun.awt.image.ImageRepresentation.setPixels(ImageRepresentation.java:528)
位于sun.awt.image.ImageDecoder.setPixels(ImageDecoder.java:120)
在sun.awt.image.PNGImageDecoder.sendPixels(PNGImageDecoder.java:531)
位于sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:452)
位于sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246)
位于sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
运行(ImageFetcher.java:136)
线程“Animation thread”java.lang.OutOfMemoryError中出现异常:java堆空间
正在处理.core.PImage。(未知源)
在处理时.core.PApplet.loadImageMT(未知源)
at processing.core.PApplet.loadImage(未知源)
at processing.core.PApplet.loadImage(未知源)
在guicomponents.GImageButton.getImages(GImageButton.java:136)
在guicomponents.GImageButton.(GImageButton.java:100)
在gimage_demo.draw(gimage_demo.java:35)
正在处理。核心。PApplet。handleDraw(未知来源)
at processing.core.PApplet.run(未知源)
运行(Thread.java:619)
为什么会发生这种情况?当数组在
draw()
结尾超出作用域时,内存不是被垃圾收集吗

我在尝试使按钮抽动时遇到了这个问题。我找不到改变它位置的方法,所以我只是在我想要的新位置创建了一个新的。有更好的方法吗

为什么会发生这种情况?当数组在draw()结尾超出作用域时,内存不是被垃圾收集吗?

唯一可以保证的是
按钮
数组在超出范围时有资格进行垃圾收集。对于
GImageButton
对象及其成员,这取决于
GImageButton
构造函数有哪些副作用

确保构造函数没有在第一个参数上“注册”自己(在您的例子中是this),并且没有缓存,或者
GImageButton
没有泄漏构造函数中的
引用,等等。

查看代码(http://code.google.com/p/gui4processing/source/browse/trunk/GUI4Processing/src/guicomponents/G4P.java?r=331)显示所有
GImageButton
实例都存储在静态HashMap中。我看不到处理它们的方法(尽管我没有那么努力)。因此它们不符合垃圾收集的条件

我希望答案是创建一次按钮(例如,在
设置中
),然后在
绘制中使用它们(除非您能找到与
处置()方法等效的方法)