Garbage collecting a BufferedImage
I am having problems reclaiming memory consumed by a (supposedly
GC'ed) BufferedImage. When I want to dispose of an image, I basically
img = null;
I was expecting this to free the memory from all resources associated
with this BufferedImage. Unfortunately it does not seem to be the
case. After some time I get a java.lang.OutOfMemoryError: Java heap
space. Manual calls to System.gc() do not change anything to the
current memory consumption. I ran tests with a pretty big heap size (-
Xmx1024M). I monitored the memory consumption using Runtime.getRuntime
().totalMemory() and Runtime.getRuntime().freeMemory().
Am I missing something?
Some more details:
This problem occurs both when I create a BufferedImage through
javax.imageio.ImageIO.read(), and when I create one directly with a
BufferedImage constructor. I noticed it while writing a simple
slideshow application which I tested with pretty big images
(2048x1536 on average). At first I thought this was a bug specific to
the Mac OS X JVM: on one hand, if I load the images using
javax.swing.ImageIcon, everything is fine because I get an instance
of apple.awt.OSXImage, which seems to be correctly GC'ed. But on the
other hand, if I load the image with ImageIO.read(), I get a
BufferedImage, which is not GC'ed when I "kill" it as described above.
Then I tried on Windows XP with Java 1.6.0_02, and the same behavior
occurs: instances of sun.awt.image.ToolkitImage are correctly GC'ed,
whereas BufferedImage instances are not.
I looked on Google, on Sun's bug database, and on Apple's java-dev
mailing list. The only thing I could find was this thread , but
which apparently does not give an answer to the problem.
To unsubscribe, send email to email@example.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
firstname.lastname@example.org and include in the body of the message "help".