0

How can I get the memory used by an instance of the class java.awt.Image?

There are other similar questions, but is there an easier way of calculating the memory used by an object of a specific class (java.awt.Image)?

I would prefer to get the amount of bytes used programatically and not via a profiler so that I can use it to calculate memory statistics inside my application.

Thanks in advance.

Community
  • 1
  • 1
ughzan
  • 1,548
  • 1
  • 14
  • 24
  • 1
    Create about 1000 of them and see how much heap grows. Do it a second time with a different image size and calculate the equation. Should be roughly a linear equation -- `k1 + k2 * num_pixels` -- for a given color depth. – Hot Licks Feb 15 '12 at 13:03

1 Answers1

3

This will vary between implementations of java.awt.Image, because it depends how the subclass decides to store the data.

But for BufferedImage, img.getData().getDataBuffer(); will give you the raw DataBuffer that stores the image. You can then use getDataType() and getSize() to determine the capacity of the data buffer.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216