问题描述
我正在尝试确定给定的TYPE_INT_ARGB 的用途是,对于正在执行某些图像处理的程序,我可以根据图像的大小设置合理的最大堆我们将其喂入。
I'm trying to determine how much heap any given TYPE_INT_ARGB BufferedImage
will use so that, for a program which is doing some image processing, I can set a reasonable max heap based on the size of image we feed it.
我编写了以下程序作为测试,然后使用该程序确定在没有 OutOfMemoryError :
I wrote the following program as a test, which I then used to determine the least maximum heap under which it would run without an OutOfMemoryError
:
import java.awt.image.BufferedImage;
public class Test {
public static void main(String[] args) {
final int w = Integer.parseInt(args[0]);
final int h = Integer.parseInt(args[1]);
final BufferedImage img =
new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
System.out.println((4*w*h) >> 20);
}
}
(印刷值是 int []
,其中存储了 BufferedImage
的像素数据。)我期望找到的是所需的最大堆类似于 x + c
,其中 x
是数据数组的大小,而 c
是一个常量,该常量由要加载的类的大小, BufferedImage
对象等组成。这是我发现的(所有值以MB为单位):
(The printed value is the expected size of the int[]
in which the BufferedImage
's pixel data is stored.) What I expected to find was that the required max heap is something like x + c
, where x
is the size of the data array and c
is a constant consisting of the sizes of the classes which are loaded, the BufferedImage
object, etc. This is what I found instead (all values are in MB):
4*w*h min max heap
----- ------------
5 -
10 15
20 31
40 61
80 121
160 241
1.5x
非常适合观察。 (请注意,对于5MB的图像,我没有发现任何最小值。)我不明白自己所看到的内容。这些多余的字节是什么?
1.5x
is a good fit for the observations. (Note that I found no minimum for the 5MB image.) I don't understand what I'm seeing. What are these extra bytes?
推荐答案
在进一步研究中,问题似乎在于堆中的旧一代无法尽管堆中有足够的可用内存,但足以扩展以容纳图像的数据数组。
On further investigation, the problem appears to be that the Old Generation in the heap is not able to expand sufficiently to accommodate the image's data array, despite that there is enough free memory in the heap at large.
有关如何扩展旧一代的更多详细信息,请参见。
For further details about how to expand the Old Generation, see this question.
这篇关于为什么BufferedImage所需的内存超出其数据数组的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!