[英]I have an OutOfMemory error when reading images
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. 好吧,所以我正在读取.jar文件中名为“ images”的子目录中的图像,当然,我必须通过流访问它们,因为它位于.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.
以前,我有一个单独的小类完成了同样的工作,但是没有导致OutOfMemoryError。
The error comes from the line with i = ImageIO.read(stream);
错误来自以下行:
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?
任何人都可以帮助找出解决错误的最佳方法吗?
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.
即使在32M或64M堆大小的情况下,也无法用十二个PNG代表简单棋子的大小(要在游戏中使用)来占用足够的空间以耗尽内存。
If you know that you must do a lot of IO (ie, reading a whole bunch of images), you can always increase the total heap consumed by your java application. 如果您知道必须做大量的IO(即读取一堆图像),则始终可以增加Java应用程序消耗的总堆。
an example to do that would look like this: 一个执行此操作的示例如下所示:
java -Xms128m -Xmx256m YourClassNameHere
java -Xms128m -Xmx256m YourClassNameHere
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.
在优化解决方案并确保不浪费内存并且应用程序确实需要使用额外的内存之后,我将把此解决方案用作最后的选择。
The best way to diagnose OOMs is to use a profiler. 诊断OOM的最佳方法是使用探查器。 JProfiler is pretty good and I believe it has a free trial period.
JProfiler相当不错,我相信它有一个免费试用期。
Also, remember that OOMs are often misleading. 另外,请记住,OOM经常会引起误解。 Just because the OOM is thrown at one particular line doesn't actually mean that's the line that uses a lot of memory;
仅仅因为OOM被扔在某一行上并不意味着那是占用大量内存的那一行。 it's just the line that used the last bit of available memory.
只是使用了最后一部分可用内存的那一行。 For example:
例如:
byte[] unused = new byte[availableMemory - 1];
byte[] reallyImportant = new byte[10]; // throws OutOfMemoryError
Image.read()
caches the image data in memory/disk. Image.read()
将图像数据缓存在内存/磁盘中。
Have answered a similar question. 已经回答了类似的问题。 Please Check here
请在这里检查
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.