Animation: *.gifs flood memory

Good morning

I wrote an applet with 18 sets of animated images. One set consists
of 22 images (.gif, each image is 2K in size). The user can repeatedly load as
many sets as he desires by the click of a button. The thing now is that
apparently each time a new set is loaded it is uncompressed (from .gif)
and stored in the users memory. Very soon the users memory is full and
warnings about memory lack pop up. Or even worse the applet stops working.

I am using a media tracker. Each time the user loads a new set by the click of a button, the old set gets 'disconnected' from the variable myImages[i], but still remains in the memory. I actually only need one set of images present.

Question: What can I do?
Can I remove the "swimming" (in the memory) image files form the memory by
java code?

I very much appreciate your time, "Mug"

Some code:

/* loadImg */
    public void loadImg( int iG, int iV, int iP, int
                         iR) {
        //set img name
        imgFileName = ...
        for (int i=0; i < numOfImages; i++) {
            String fullImgName = imgDirName +"/"+ imgFileName + i +".gif";
            myImages[i] = getImage(getDocumentBase(), fullImgName);
        //media tracker
        try { myTracker.waitForAll(); this.showStatus("Loading images
        done"); start();}
        catch (InterruptedException e) {
            System.out.println("An exception occurred: " +

    /* update */
    public void update(Graphics g) {

    /* paint */
     public void paint(Graphics g) {

Who is Participating?

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

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.

Couple of things you can do: first, call garbage collector after loading a new set of images (it's sufficient for MS IE, but Netscape is reluctant to satisfy GC requests); second, free image resources before loading a new image:
for (int i=0; i < numOfImages; i++) {
  String fullImgName = imgDirName +"/"+ imgFileName + i +".gif";
  if(myImages[i] != null)
  myImages[i] = getImage(getDocumentBase(), fullImgName);

As I said, this works fine in IE but poorly in Netscape 3.0. I haven't tried it in the final release of Communicator yet, but I'm going to in the next couple of days and will let you know.


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
mugAuthor Commented:
Thank you for answering my question. I work on a Widows NT PC (Dell 180 MHz, 32 MB RAM). When I use the appletviewer (IDE Symantec Cafe 1.51) the flushing (myImages[i].flush()) helps a little bit. That means some part of the old images must have been flushed, but still the memory load grows with every new loaded set of images, same with Netscape 4.0. I still have to try MS IE.

Regards, "mug"
I had a similar problem. I noticed that 'flush'ing and 'gc'ing helped a bit but not significant enough to prevent my machine from giving me an OutOfMemoryError.

My way of working around it was to update my code to JDK1.1 so that I could use the tracker.removeImage() method that successfully clears the image from the memory. This with some gc() calls did the job.

My code now works succesfully in various environments - UNIX, SGI, Windows95 and Mac.
mugAuthor Commented:
Thanks! I tried to update my code to JDK1.1.3. But it takes more than a simple recompiling. So I postpone this task to later.
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

From novice to tech pro — start learning today.