We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Memory leak in image display

faust073197 asked
Medium Priority
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?
Watch Question

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.


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?


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)

      public void main (String args[])
            java_app application = new java_app("java_app");
            application.resize(300, 300);
      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)
             System.out.println(simage + x);

                 return true;


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?

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.


i've tried running System.gc() right after the image.flush() with no result

Try to run System.gc() after 'image = null'.
This question has been answered, see the query "Images and flushing memory", answered by keisha on 09/09

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.