ImageIcon memory sizes

L

Luke

I have a large .jpg file, about 2.7M, which I'm converting to an
ImageIcon. I'm trying to find out how much memory the ImageIcon is
using with a code snippet like this,

byte[] b = ... the .jpg data
System.out.println(Runtime.getRuntime().totalMemory());
ImageIcon icon = new ImageIcon(b);
System.out.println(Runtime.getRuntime().totalMemory());

It appears that the ImageIcon is using over 60M of memory. Can this be
possible?

My goal is to display an image in a JScrollPane. I'm creating a JLabel
and setting it's icon to the ImageIcon. This is apparently taking
*lots* of memory. When I get a chance I'm going to try the alternate
approach of creating an Image and then using a Graphics.drawImage() call
to paint the image myself. I hope this will use much less memory.
 
R

Roedy Green

It appears that the ImageIcon is using over 60M of memory. Can this be
possible?
Internally I would expect icons to be floating around is some raster
format or format friendly to your video card. Java throws a bit of a
magician's silk handkerchief over exactly how it works inside. Or I
could put it this way, most people use the system without having the
remotest inkling of how it works.

To see the problem, try converting jpg to tiff or some uncompressed
raster format using a tool like the Gimp or PaintShopPro.

You will see them balloon preposterously. Why?

1. JPG is a compressed format.

2.JPG's view of the world has nothing to do with pixels. When you
render JPG it looks at its internal photograph, blows it up to the
desired size and starts calculating pixels by interpolation. This is
not literally true, but you could imagine JPGs consisted only a few
sample points at key places with their colours, and all the rest is
filled in on demand by interpolating. The raster of course has all
the interpolation data.

3. internally Java usually works with 32 bits per pixel. There are 8
bits each of red, blue, green and alpha (transparency/opacity). JPG
does not have opacity and typically would not measure colours that
accurately.
 
A

Andrew Thompson

..When I get a chance I'm going to try the alternate
approach of creating an Image and then using a Graphics.drawImage() call
to paint the image myself. I hope this will use much less memory.

That is probably the better way to go about what you
are doing, I cannot speak for the size of Images as opposed
to ImageIcons, but Java is generally horrendous when iy comes
to image representations, as well as *caching* (very relevant
if you intend to load more than one image at any time during
the course of your application).

I found the nes approach was to load the image bytes
yourself and use Toolkit.createImage() to stamp out an
Image as required.

I see 'Ex-Guardian Reader' has given you a tip re 'catch'ing
your OutOfMemory*Error*s, but I'll just add that it would
have been easier to determine that you were..

try {
// images stuff
}
catch (Exception e) {
// won't catch an 'Error'!
....

If you had posted an SSCCE. <http://www.physci.org/codes/sscce.jsp>

You might also Google this group for recent discussions
about what to do once you have caught those OOME's.

HTH
 
J

Joan

Luke said:
I have a large .jpg file, about 2.7M, which I'm converting to an
ImageIcon. I'm trying to find out how much memory the
ImageIcon is
using with a code snippet like this,
<snip>
I looked at a little maybe a year ago and it is possible that
java
is keeping more than one version of your ImageIcon, one in raw
format and one in rgb maybe. Plus there is no compression for
these
like jpg.
 
J

jan V

It appears that the ImageIcon is using over 60M of memory. Can this be
possible?

What's the dimension of your image? Many modern digital cameras with say 6
megapixel image resolution require some 24M of RAM to store the picture
(uncompressed, of course). 8 MP images require 32M. 60M would seem grossly
excessive if your pic was an 8MP one..
*lots* of memory. When I get a chance I'm going to try the alternate
approach of creating an Image and then using a Graphics.drawImage() call
to paint the image myself. I hope this will use much less memory.

Identify the cause of this huge footprint first, then maybe you'll be able
to tweak something simple to greatly reduce the footprint. If that doesn't
work, then think of alternative rendering approaches.
 
A

Andrey Kuznetsov

I have a large .jpg file, about 2.7M, which I'm converting to an
ImageIcon. I'm trying to find out how much memory the ImageIcon is
using with a code snippet like this,

byte[] b = ... the .jpg data
System.out.println(Runtime.getRuntime().totalMemory());
ImageIcon icon = new ImageIcon(b);
System.out.println(Runtime.getRuntime().totalMemory());

It appears that the ImageIcon is using over 60M of memory. Can this be
possible?

My goal is to display an image in a JScrollPane. I'm creating a JLabel
and setting it's icon to the ImageIcon. This is apparently taking
*lots* of memory. When I get a chance I'm going to try the alternate
approach of creating an Image and then using a Graphics.drawImage() call
to paint the image myself. I hope this will use much less memory.
No

I memorize that AWT has following bug: memory use for _first_ loaded image
was twice as big as needed.
The workaround is simple, first load small image 1x1, then your image.
 
R

Roedy Green

Identify the cause of this huge footprint first, then maybe you'll be able
to tweak something simple to greatly reduce the footprint. If that doesn't
work, then think of alternative rendering approaches.

Consider also that if you created several objects on your way to the
ImageIcon, even if they are unreachable, they will still show up in
your ram footprint, the way you measured it.. You can try a
System.gc() before each measure and see if it reduces the apparent
footprint.

A memory profiler might give you a more accurate picture.

In other words, it might not be as awful as it appears.

I would love it if someone could write an essay, analogous those ones
they showed you in elementary school, about how trees turn into
pencils, on how ram images make it to your screen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top