United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6687968 PNGImageReader leaks native memory through an Inflater.
JDK-6687968 : PNGImageReader leaks native memory through an Inflater.

Details
Type:
Bug
Submit Date:
2008-04-14
Status:
Resolved
Updated Date:
2011-01-19
Project Name:
JDK
Resolved Date:
2009-01-09
Component:
client-libs
OS:
linux,solaris_10
Sub-Component:
javax.imageio
CPU:
x86,sparc
Priority:
P2
Resolution:
Fixed
Affected Versions:
5.0,6,6u7
Fixed Versions:

Related Reports
Backport:
Backport:
Backport:
Backport:
Backport:
Relates:

Sub Tasks

Description
FULL PRODUCT VERSION :
java version "1.6.0_04"
Java(TM) SE Runtime Environment (build 1.6.0_04-b12)
Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Any OS which exhibits Bug #4797189.

A DESCRIPTION OF THE PROBLEM :
Line 1228 of PNGImageReader creates a new Inflater and passes it into an InflaterInputStream:

            is = new InflaterInputStream(is, new Inflater());

Inspection of the InflaterInputStream code reveals that this Inflater will never be "end"ed, even by a call to "close", causing a leak of memory as described in Bug #4797189.

A reference to the Inflater should be maintained and properly "end"ed in the "PNGImageReader .dispose" method.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached program to reproduce.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Expected normal heap usage.
ACTUAL -
Actual heap usage mimics that of Bug #4797189.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.File;

import javax.imageio.ImageReadParam;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.ImageInputStream;

import com.sun.imageio.plugins.png.PNGImageReader;
import com.sun.imageio.plugins.png.PNGImageReaderSpi;

public class Leak
{
    public static void main(String[] args) throws Exception
    {
        while (true)
        {
            ImageInputStream stream = new FileImageInputStream(new File("image.png"));
            PNGImageReader imageReader = new PNGImageReader(new PNGImageReaderSpi());
            imageReader.setInput(stream);
            imageReader.read(0, new ImageReadParam());
            imageReader.dispose();
            stream.close();
        }
    }
}

---------- END SOURCE ----------

                                    

Comments
EVALUATION

Its not strictly a leak, its less than prompt finalization
The code that creates the Inflater instance is presumably doing so precisely so
that it can release that resource in a timely manner without closing the
client-supplied input stream, but it fails to do.

When not explicitly providing an Inflater instance InflaterInputStream instances
need to be close()'d asap to release the Inflater
instance. This needs to be done in one other place in PNGImageReader.
                                     
2008-07-22
SUGGESTED FIX

http://sa.sfbay.sun.com/projects/java2d_data/7/6687968.0
                                     
2008-07-25



Hardware and Software, Engineered to Work Together