Java 读取图像 - java.lang.OutOfMemoryError
我尝试使用以下代码读取一个 10MB Jpeg 图像,但出现“线程“main”java.lang.OutOfMemoryError 中的异常:Java 堆空间”。我想知道10MB的图像是否太大而无法读取,如果是的话我是否应该增加堆空间。
增加堆空间应该是最后的选择,所以我想知道是否有其他方法可以实现这一点。我的代码附在下面;
BufferedImage image;
try {
image = ImageIO.read(new File("C:/Users/Administrator/Desktop/aa.jpg"));
printDetails(image);
} catch (IOException e) {
}
帮助表示赞赏。
There is a 10MB Jpeg image i am trying to read using the following code, but i get a "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space". I wonder if a 10MB image is too big to be read, should i increase the heap space if so.
Increasing the heap space should be the last alternative, so i wonder if there's any other way i could achieve this. My code is attached below;
BufferedImage image;
try {
image = ImageIO.read(new File("C:/Users/Administrator/Desktop/aa.jpg"));
printDetails(image);
} catch (IOException e) {
}
Help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,10MB JPEG 可以轻松解压缩为更多兆字节的未压缩图像数据。
假设您使用标准 24 位色彩空间(RGB 每个 8 位)并且图像尺寸为 10000 x 10000 像素(考虑到 10MB 输入大小,这并不是太不现实),那么您需要超过 286MB存储来存储原始的、未压缩的图像数据(假设开销为零)。
这明显大于 Oracle 客户端 VM 的默认最大堆大小,因此您可能需要使用
-Xmx
参数。Note that a 10MB JPEG can easily be decompressed to many more megabytes of uncompressed image data.
Assuming you're using a standard 24bit colorspace (8 bit for RGB each) and the image has dimensions of 10000 x 10000 pixels (which isn't too unrealistic, given the 10MB input size), then you'd need more than 286MB of storage to store the raw, uncompressed image data (and that's assuming that there is zero overhead).
That's significantly larger than the default maximum heap size of the Oracle client VM, so chances are that you'll need to increase the available heap memory using the
-Xmx
argument.