Memory leak in image display

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?
faust073197Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mann061997Commented:
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
faust073197Author Commented:
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
faust073197Author Commented:
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
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

mann061997Commented:
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
arkturCommented:
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
faust073197Author Commented:
i've tried running System.gc() right after the image.flush() with no result
0
aleshkaCommented:
Try to run System.gc() after 'image = null'.
0
adrianr091197Commented:
This question has been answered, see the query "Images and flushing memory", answered by keisha on 09/09
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.