处理:内存不足错误
我正在使用处理。这是我的整个草图:(
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()
结束时超出范围时,内存不会被垃圾回收吗?
我在尝试使按钮抽动时遇到了这个问题。我找不到更改其位置的方法,因此我只是在我想要的新位置创建了一个新位置。有更好的方法吗?
I'm using Processing. Here is the entirety of my sketch:
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)
};
}
(It is just a demo to illustrate the issue I'm having.) If this runs for long enough, an OutOfMemoryError
will be generated:
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)
Why could this be happening? Doesn't the memory get garbage collected when the array passes out of scope at the end of draw()
?
I encountered this problem when trying to make a button twitch. I couldn't find a way to change its location, so I just created a new one at the new location I wanted. Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
唯一可以保证的是
buttons
数组在它被垃圾回收的时候符合垃圾回收的条件。超出范围。对于 GImageButton 对象及其成员,这取决于 GImageButton 构造函数具有哪些副作用。确保构造函数不会在第一个参数(在您的情况下为
this
)上“注册”自身,并且没有进行缓存,或者GImageButton
没有进行缓存。不要从构造函数等内部泄漏它的this
引用。The only thing guaranteed is that the
buttons
array is eligible for garbage collection when it passes out of scope. For theGImageButton
objects, and it's members, it depends on what side-effects theGImageButton
constructor has.Make sure the constructor doesn't "register" itself on the first argument (
this
in your case), and that there's no caching going on, or that theGImageButton
doesn't leak it'sthis
reference from within the constructor, etc.查看代码 (http://code.google.com/p/gui4processing/source/browse/trunk/GUI4Processing/src/guicomponents/G4P.java?r=331) 显示所有
GImageButton
实例存储在静态 HashMap 中。我看不到处理它们的方法(尽管我没有那么努力)。因此他们没有资格进行垃圾收集。我希望答案是创建一次按钮(例如在
setup
中),然后在draw
中使用它们(除非您可以找到dispose() 的等效项
方法)。Looking at the code (http://code.google.com/p/gui4processing/source/browse/trunk/GUI4Processing/src/guicomponents/G4P.java?r=331) shows that all
GImageButton
instances are stored in a static HashMap. I can't see a way to dispose of them (although I haven't looked that hard). Therefore they won't be eligible for Garbage Collection.I expect the answer is to create the buttons once (e.g. in
setup
) and then use them indraw
(unless you can find the equivalent of adispose()
method).