Solved

Memory leak in image display

Posted on 1997-07-31
8
238 Views
Last Modified: 2006-11-17
I have slideshow application that displays numerous images. However, after each image display more memory is taken by the program till after about 40 images the program runs out of memory and crashes.  I need to be able to clear the memory used by each previously displayed image.  I have tried g.dispose() and image.flush() with no results.  How do I reclaim the used memory?
0
Comment
Question by:faust073197
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Expert Comment

by:mann061997
ID: 1224227
Remember that MediaTracker will maintain a reference to registered images - they can't be garbage collected until they are released or the Tracker object goes out of scope.
0
 

Author Comment

by:faust073197
ID: 1224228
I have tried it with elimination of the MediaTracker, with no effect... still how would one release a reference to a image in MediaTracker... just set it to null?
0
 

Author Comment

by:faust073197
ID: 1224229
Actually, here is a simple example of a program that illustrates my problem.  Each mouse click displays a new image, but each previous image still exists in memory.  After 40 or so the program runs out of memory and crashes.


import java.awt.*;
import java.util.*;
import java.io.*;

public class java_app extends Frame
{
      Image image;
      int x = 10;
      String simage = "back.jpg";
      Toolkit tk = Toolkit.getDefaultToolkit();
        
      public java_app(String title)
      {
            super(title);
      }

      public void main (String args[])
      {
        
            java_app application = new java_app("java_app");
            application.resize(300, 300);
            application.show();
      }
      public void paint(Graphics g)
      {
                 String simage =  "img00" + x + ".jpg";
             image =  tk.getImage(simage);
            g.drawImage(image, 0 , 0, this);
            Runtime info = Runtime.getRuntime();
            System.out.println("total memory= " + info.totalMemory());

            
      }

      public boolean mouseDown(Event evt, int xx, int yy)
{
            if (image != null)
            {image.flush();}
             repaint();
             System.out.println(simage + x);

             x++;
             repaint();
                 return true;
      }

}


0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 1

Expert Comment

by:mann061997
ID: 1224230
flush() only marks the image for deletion; it doesn't get removed until the next gc cycle. Try calling System.gc() after flush(). Does it make any difference?
0
 

Expert Comment

by:arktur
ID: 1224231
If you use Sun's JVM try to run your application with by calling "java -verbosegc your_application_class". You will be able to see when gc runs.
0
 

Author Comment

by:faust073197
ID: 1224232
i've tried running System.gc() right after the image.flush() with no result
0
 
LVL 2

Expert Comment

by:aleshka
ID: 1224233
Try to run System.gc() after 'image = null'.
0
 

Accepted Solution

by:
adrianr091197 earned 150 total points
ID: 1224234
This question has been answered, see the query "Images and flushing memory", answered by keisha on 09/09
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question