java - 处理:内存不足错误

标签 java user-interface processing out-of-memory

我正在使用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()结束时超出范围时,内存不会被垃圾回收吗?

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

最佳答案

Why could this be happening? Doesn't the memory get garbage collected when the array passes out of scope at the end of draw()?

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

确保构造函数不会在第一个参数(在您的情况下为 this)上“注册”自身,并且没有进行缓存,或者 GImageButton 不会从构造函数等内部泄漏它的 this 引用。

关于java - 处理:内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5810191/

相关文章:

java - 具有 2 个参数的静音命令 (ArrayIndexOutOfBoundsException)

UIViewController 内的 iOS 自定义 UITableViewController

java - 将字符串转换为浮点型 ->Processing.org

每 40 毫秒处理一次 saveFrame

java - 是否可以在 Java 中仅 check out cvsclient 中的目录结构?

java - 无法将xmlReader转换为解析器

用于丰富 gui 界面的 Python 模块

c# - WPF 中是否有 C# 的 MVC 模式

javascript - 在处理中访问嵌套的 JSON 数组

java - 在 Weblogic 10.3 中调用 EJB 时出现 NameNotFoundException