Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Memory Leak in a multi threaded Swing application (ImageIcon)

843805Jan 9 2007 — edited Jan 11 2007
When I profile my swing application with netbeans 5.5 , I notice that after each periodically tidy up my image container (add,remove IconImage objects to a hashmap or arraylist), there gather by and by surviving objects. This leads to run out of memory after a while. Is there any other way to avoid this effect?

Any ideas about "The JVM is notorious for caching images."
please help..

what I have made briefly:

1.) Read the binary stream from the db :
rs=stmt.executeQuery(query);
if(rs.next()){
    int len=rs.getInt(2);
    byte [] b=new byte[len];
    InputStream in = rs.getBinaryStream(3);
    try {
            in.read(b);
            in.close();
            img=Toolkit.getDefaultToolkit().createImage(b);
     } catch (IOException e) {
            e.printStackTrace();
    }
}

stmt.close();
rs.close();
2.) hold the icon as field :
this.icon =  new ImageIcon(img);
3.) After a while I remove the object from my collection and on the
overridden method finalize() I also call the flush() method.
if(this.icon != null){
            this.icon.getImage().flush();
            this.icon = null;

}
The surviving objects still increase?! On the page of SUN they
submitted a bug. But this is set on closed/fixed.

(http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4014323)

What am I doing wrong? I also use the byte[] constructor on creating the icon and
my java version is 1.5.0_10.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 8 2007
Added on Jan 9 2007
25 comments
378 views