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.
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 usegetDataType()andgetSize()to determine the capacity of the data buffer.