views:

67

answers:

1

I'm trying to apply an AffineTransform to an image in Java. Here's the code that I'm running.

AffineTransformOp op =
        new AffineTransformOp(atx, interactive ? interpolationInteractive : interpolationNormal);
displayImage = op.filter(displayImage, null);

Where atx is a valid AffineTransform object. This code seems to run fine, but it leaves a very large amount of memory around after I do it a number of times, and eventually my program runs out of memory.

I'm sure this line is the culprit, because if I comment out applying the transformation, then no memory leak occurs.

I have 3 questions:

  1. Why is the syntax for the filter method so strange (it takes in a src, and destination, and returns a destination)?
  2. Why does this cause a memory leak?
  3. How can I fix it?

Thanks!

+2  A: 

BufferedImage, depending on the resolution of the source, may be quite large and expensive to create. With that in mind:

  1. .filter() method syntax takes destination parameter to avoid unnecessary creation of BufferedImage copy if you already have one ready. In other words, if I need to process a sequence of (compatible) images, reusing one buffered image as destination for all transformations would be a better approach then creating a new one each time.

  2. Transformation itself does not cause a memory leak. You directly or indirectly holding onto all the BufferedImage instances you've created does.

  3. That depends on what you're doing. For starters, you need to find out where your BufferedImage instances are being referenced. Calling flush() won't hurt either.

ChssPly76
I just revisited this issue, and attempted to use the other form of the filter() method. However, I've noticed that when I use this form it is much slower than the image = op.filter(image, null). Any ideas why?
Jon
The "other" form being the one where you pass in `BufferedImage` destination? It should not be slower; certainly not much slower. Are you timing the actual `filter()` operation or some surrounding code as well? Or do you mean `Raster`-based by "other"? I don't think that should be much slower either but I've never used it.
ChssPly76