2

I am having strange problems with BufferedImage, which in some cases consumes all the free system memory (3GB, 1.5GB free).

I have created a simple wrapper and I use it like this:

public ImageWrapper(final byte[] bytes) throws ImageWrapperException {
    this(new ByteArrayInputStream(bytes));
}



public ImageWrapper(final ByteArrayInputStream bis) throws ImageWrapperException {
    try {
        image = ImageIO.read(bis);
        bis.close();
    } catch (IOException e) {
        throw new ImageWrapperException(e);
    }
}

(I have jsut verified that it happens even with image = ImageIO.read(file);)

I am not getting any exceptions until the first "Cannot allocate memory".

For some reason, when reading specific type of images, the reading of the image will end up with all the system memory consumed. I am not talking about heap, but really the system memory.

It happens only in certain environments - it does not happen on my OSX, but it happens on my Debian server.

  1. Do you have an idea why this could be happening?
  2. Are there any alternatives to BufferedImage, possibly working better?
  3. The problematic machine is a Virtual Server. Can it be caused by its configuration?

Thanks

EDIT:

  1. Example image: http://cl.ly/1P430l0V1g133r0C291J
  2. It is just the first and only instance which will produce this.
  3. I have just verified that it also happens with: image = ImageIO.read(file); - I am starting to think, that it must be something outside of Java - some native library which is buggy...

EDIT2:

So the problem is with the FileSystem - where I have a 7GB directory with thousands of images inside. When I try to read a file, it consumes all the memory - I suppose it is some kind of Filesystem issue.

Vojtěch
  • 11,312
  • 31
  • 103
  • 173
  • What specific type of image? Can you post a binary 1:1 copy of one such image? How do you use `ImageWrapper` in your surrounding code, how many instances, etc.? Post some more code please. – Philipp Reichart Jun 05 '12 at 23:17
  • Especially if you resize, split the ImageIO.read into using the first ImageReader available for the image type. This circumvents the BufferedImage memory usage. BTW. do bis.close() in a finally block. – Joop Eggen Jun 05 '12 at 23:29
  • Philipp: See my updates. Joop: This happens even without any resizing or saving. Even if I did not do bis.close(), I don't see any reason, why the process would consume 1.5 GB of memory. – Vojtěch Jun 05 '12 at 23:33
  • 1
    @Vojtěch: how *exactly* do you measure the memory usage? Memory measurements on Linux are not as straight-forward as they seem... – thkala Jun 05 '12 at 23:37
  • 1
    Are you saying that if you write a class that has a main method public static void main(String[] args) { Image image = ImageIO.read(args[0]); } And pass in the filename of the image you linked, it reproduces this for you? – John Watts Jun 05 '12 at 23:38
  • I haven't had time to test it separately, but that is the moment, when my application and also my server gets completely out of memory. Will try to do that today. – Vojtěch Jun 07 '12 at 05:42
  • thkala: I don't measure it - when it happens, I can't even write "ls" in bash as even Bash complains about "Cannot allocate memory". – Vojtěch Jun 07 '12 at 05:43
  • Weird... have you confirmed this with a non-Java program? E.g. using `dd` or `cat`? I'd be really interested to know the cause of this issue. – thkala Jun 08 '12 at 11:36
  • Non-java problems are okay - even reading the file with Java seems to be okay. The problem is really with the BufferedImage :/ – Vojtěch Jun 08 '12 at 15:38

3 Answers3

3

There are some known bugs related to ImageIO.read() and BufferedImage

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7166379

http://bugs.sun.com/view_bug.do?bug_id=6716560

1

There is definitely something wrong with BufferedImage - I've tested it on two servers and it was leaking with the same results - System completely out of memory.

In the end I've written a simple wrapper over PHP and I now use GD for image manipulation. Works fine now. Thanks for all the suggestions!

Vojtěch
  • 11,312
  • 31
  • 103
  • 173
0

Try moving the code to java.nio and memory mapped file access. Those are stored outside the heap.

This SO is interesting.

Community
  • 1
  • 1
ssedano
  • 8,322
  • 9
  • 60
  • 98
  • The problem was not in the heap, there was some problem in native code as the whole system was running out of memory. – Vojtěch Mar 17 '13 at 09:49