1

Today I'm playing with a method, which crop and resize an image with resolution of 3264x2448.

The JVM's heap space length is only 64mb.

I always get the "memory leak" when I load this image using the method:

BufferedImage bImage = ImageIO.read( stream );

There are other way to resize it without go in memory leak, or the only problem is the little size of heap space?

This image's size is 3mb, why it take 50mb in heap space?

Thanks in advance, sorry if my question is stupid but it's strange for me

Byee

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
Andrea Catania
  • 1,361
  • 3
  • 21
  • 37

3 Answers3

1

1) Image size in file is the size of structured data, compressed (e.g. in jpeg format) in file on the disk.

2) Data in memory is object representation of your image, so it could be much more greater then image resources in file.

3) There isn't way how to solve your problem without increasing memory space. Look at this: What does Java option -Xmx stand for?

Community
  • 1
  • 1
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • Very thanks for your answer, You have confirmed what I fear :). this method run in tomcat, and get the image from a form, there are any way to block the upload of this image if the size is more than 2mb? – Andrea Catania Jan 31 '14 at 10:52
  • @AndreaCatania Sure. Look at this answer: http://stackoverflow.com/questions/8091428/setting-file-size-restrictions-when-uploading-files-with-jersey It is about `jersey`, but answer can be applied to the `tomcat` too. – Andremoniy Jan 31 '14 at 10:54
  • ohh! good but how can I stop the upload after reach max size limit? Can I use "Image Magic" for avoid the problem? – Andrea Catania Jan 31 '14 at 11:05
  • Just close a stream and that's all folks. – Andremoniy Jan 31 '14 at 11:12
  • Count size while reading stream. Use for example http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/CountingInputStream.html – Andremoniy Jan 31 '14 at 11:32
1

Sometime ago I was struggling with the same question. I might say, it was a tough time, trying to load image into the memory, because there were no limit to how big an image can actually be. So, I went with Image Magic solution. It has its own drawbacks thou: it was consuming quite a lot of my PC resources, when it had to work with a big image (but I assume it could be tuned, didn't actually spend much time on it).

Basically, if in the end you don't need the image to be loaded into your Java code (for example, for preview), then image magic is really good solution for that. If you still need a preview to show to a user, then I was using external program to open an image (or actually PDF in my case, but I think you can use default OS image viewer for that matter).

Community
  • 1
  • 1
Alex K.
  • 3,294
  • 4
  • 29
  • 41
  • 1
    Thanks a lot for your answer, I had read your post but my method is running on a servlet in tomcat. I can use Image Magic? it's a library or i need install it in SO like a program? – Andrea Catania Jan 31 '14 at 10:56
  • Eeverything is possible:) ImageMagic is just a set of command line/shell tools. If you install it on server you can execute it from servlet. http://stackoverflow.com/questions/15847231/processbuilder-start-returns-0-but-doesnt-execute-shell-script – Alex K. Jan 31 '14 at 11:27
  • "Eeverything is possible" good answer :D so "image magic" don't use the heap space so I can pass trough the memory leak ? the only problem is that if the hosting can install that tool right? – Andrea Catania Jan 31 '14 at 12:26
  • That is right. There will no be any problems at least with your application heap space:) Thou it will still consume your host resources, like CPU and RAM. – Alex K. Jan 31 '14 at 12:29
1

This image's size is 3mb, why it take 50mb in heap space?

The image is compressed, in memory it is uncompressed, which means in your case:

 3264 * 2448 * 4 = 31 MB // 4 byte per pixel
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110