Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

Memory Leak in a multi threaded java application (ImageIcon)

Thread view: 
cakmak - 10 Jan 2007 10:37 GMT
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." I stripped-down my code, this also
gathers surviving objects. An application containing this code could
not live long.

public class LeakCheck {
   private int counter=0;
   private final String randomquery = "SELECT TOP 4 T00801PersonNr AS
PersonNr,T00801PersonID AS PersonID, T00801Nachname AS
Nachname,T00801Vorname AS Vorname FROM DBA.T00801Person ORDER BY
RAND()";

   private class Person extends Object {
       private javax.swing.ImageIcon icon;
       private String number;
       protected void finalize() throws Throwable {
           if(icon!=null){
               icon.getImage().flush();
               icon = null;
           }
           super.finalize();
       }
   }
   private ArrayList<Person> personList = new ArrayList<Person>();

   protected class RefreshTask extends TimerTask {
       @Override
       public void run() {
           random();
       }
   }
   public void go(){
       Timer updateTimer = new Timer("Refresh Task");
       updateTimer.schedule(new RefreshTask(),0,2000);
   }
   private Image LoadImageFromDB(String personnr){
       Image img = null;
       String filename = personnr + ".jpg";
       Connection con = getEtrainerDBConnection();
       Statement stmt;
       ResultSet rs;
       try {
           stmt = con.createStatement();
           String query = Resources.getProperty("selectPersonImage");
           query = String.format(query, filename);
           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();
               }
           }
           rs.close();
           stmt.close();

       } catch (SQLException e) {
           e.printStackTrace();
       }
       return img;
   }
   public void random(){
       java.sql.Connection con = getSybaseDBConnection();
       try {
           if(con!=null && !con.isClosed()){
               java.sql.Statement stmt=null;
               try {
                   stmt = con.createStatement();
                   java.sql.ResultSet rs =
stmt.executeQuery(randomquery);
                   while(rs.next()){
                       Person person = new Person();
                       person.number = rs.getString("PersonNr");
                       Image img = LoadImageFromDB(person.number);
                       if(img !=null){
                           ImageIcon ico = new ImageIcon(img);
                           person.icon = ico;
                       }
                       personList.add(person);
                       System.out.println("Container size: " +
personList.size());
                       counter++;
                   }
                   if(counter%20 == 0){
                       personList.clear();
                       System.gc();//no need, but I force
                       System.out.println("size: " +
personList.size());
                   }
                   rs.close();
                   stmt.close();
               } catch (SQLException ex) {
                   ex.printStackTrace();
               }
           }
       } catch (SQLException ex) {
           ex.printStackTrace();
       }
   }
   public static void main(String[] args) {
       LeakCheck leakCheck = new LeakCheck();
       leakCheck.go();
   }
}
Andrew Thompson - 10 Jan 2007 11:12 GMT
..
> ..Any ideas about "The JVM is
> notorious for caching images."

Note:  This post is the continuation of a thread
on c.l.j.g. that failed to reveal the underlying
problem.  Further details can be seen on the
thread beginning with this post..
<http://groups.google.com/groups?selm=1168248935.342086.314340@s34g2000cwa.google
groups.com
>

Andrew T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.