读取图像时出现 OutOfMemory 错误
好吧,我正在读取 .jar 文件中名为“images”的子目录中的图像,当然我必须通过流访问它们,因为它位于 .jar 中。这就是读取图像的内容:
private Image wPawn = getImage(getClass().getResourceAsStream("images/WhitePawn.png")),
bPawn = getImage(getClass().getResourceAsStream("images/BlackPawn.png")),
wRook = getImage(getClass().getResourceAsStream("images/WhiteRook.png")),
bRook = getImage(getClass().getResourceAsStream("images/BlackRook.png")),
wKnight = getImage(getClass().getResourceAsStream("images/WhiteKnight.png")),
bKnight = getImage(getClass().getResourceAsStream("images/BlackKnight.png")),
wBishop = getImage(getClass().getResourceAsStream("images/WhiteBishop.png")),
bBishop = getImage(getClass().getResourceAsStream("images/BlackBishop.png")),
wQueen = getImage(getClass().getResourceAsStream("images/WhiteQueen.png")),
bQueen = getImage(getClass().getResourceAsStream("images/BlackQueen.png")),
wKing = getImage(getClass().getResourceAsStream("images/WhiteKing.png")),
bKing = getImage(getClass().getResourceAsStream("images/BlackKing.png"));
private Image getImage(InputStream stream){
Image i;
try{
i = ImageIO.read(stream);
stream.close();
} catch (IOException e) {
i = null;
}
return i;
}
这段代码可以工作,但是运行的国际象棋游戏远远落后于我的鼠标输入。之前,我有一个小的独立类可以完成相同的工作,但它不会导致 OutOfMemoryError。
该错误来自 i = ImageIO.read(stream); 行
它是从图像声明中调用的。任何人都可以帮助找出解决该错误的最佳方法吗?
All right, so I am reading images in a subdirectory in the .jar file called 'images', and of course I have to access them via a stream because it's in a .jar. This is what reads the images:
private Image wPawn = getImage(getClass().getResourceAsStream("images/WhitePawn.png")),
bPawn = getImage(getClass().getResourceAsStream("images/BlackPawn.png")),
wRook = getImage(getClass().getResourceAsStream("images/WhiteRook.png")),
bRook = getImage(getClass().getResourceAsStream("images/BlackRook.png")),
wKnight = getImage(getClass().getResourceAsStream("images/WhiteKnight.png")),
bKnight = getImage(getClass().getResourceAsStream("images/BlackKnight.png")),
wBishop = getImage(getClass().getResourceAsStream("images/WhiteBishop.png")),
bBishop = getImage(getClass().getResourceAsStream("images/BlackBishop.png")),
wQueen = getImage(getClass().getResourceAsStream("images/WhiteQueen.png")),
bQueen = getImage(getClass().getResourceAsStream("images/BlackQueen.png")),
wKing = getImage(getClass().getResourceAsStream("images/WhiteKing.png")),
bKing = getImage(getClass().getResourceAsStream("images/BlackKing.png"));
private Image getImage(InputStream stream){
Image i;
try{
i = ImageIO.read(stream);
stream.close();
} catch (IOException e) {
i = null;
}
return i;
}
This code works, but the chess game that runs is simply terribly behind with my mouse inputs. Before, I had a small separate class that did this same job, but it did not result in an OutOfMemoryError.
The error comes from the line with i = ImageIO.read(stream);
and it is called from the image declarations. Can anyone help be figure out the best way to combtat the error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
优化/调整/压缩您的图像。即使堆大小为 32M 或 64M,代表游戏中使用的尺寸的简单棋子的 12 个 PNG 也不可能占用足够的空间来耗尽内存。
Optimize/resise/compress your images. There's no way twelve PNGs representing simple chess pieces at sizes to be used in a game should take up enough space to run you out of memory, even at 32M or 64M heap sizes.
如果您知道必须执行大量 IO(即读取一大堆图像),那么您始终可以增加 Java 应用程序消耗的总堆。
这样做的一个例子如下:
投入更多资源来解决问题永远不是解决方案,除非您知道没有办法解决它。在优化解决方案并确保不浪费内存并且应用程序确实需要使用额外内存后,我将使用此解决方案作为最后的手段。
If you know that you must do a lot of IO (i.e, reading a whole bunch of images), you can always increase the total heap consumed by your java application.
an example to do that would look like this:
Throwing more resources at a problem is never a solution unless you know that there is no way around it. I would use this solution as a last resort after you have optimized your solution and have ensured that no memory is being wasted and the application really needs to use extra memory.
诊断 OOM 的最佳方法是使用分析器。 JProfiler 非常好,我相信它有免费试用期。
另外,请记住 OOM 常常具有误导性。仅仅因为 OOM 被抛出到某一特定行实际上并不意味着该行使用了大量内存;它只是使用了可用内存的最后位的行。例如:
The best way to diagnose OOMs is to use a profiler. JProfiler is pretty good and I believe it has a free trial period.
Also, remember that OOMs are often misleading. Just because the OOM is thrown at one particular line doesn't actually mean that's the line that uses a lot of memory; it's just the line that used the last bit of available memory. For example:
Image.read()
将图像数据缓存在内存/磁盘中。曾经回答过类似的问题。 请查看此处
Image.read()
caches the image data in memory/disk.Have answered a similar question. Please Check here