Solved

Animation: *.gifs flood memory

Posted on 1997-06-12
4
1,991 Views
Last Modified: 2008-02-26
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) {
        stop();
        ...
        //set img name
        imgFileName = ...
        for (int i=0; i < numOfImages; i++) {
            String fullImgName = imgDirName +"/"+ imgFileName + i +".gif";
            myImages[i] = getImage(getDocumentBase(), fullImgName);
            myTracker.addImage(myImages[i],i);
        }
        //media tracker
        try { myTracker.waitForAll(); this.showStatus("Loading images
        done"); start();}
        catch (InterruptedException e) {
            System.out.println("An exception occurred: " +
            e.getMessage());
            e.printStackTrace();
        }
    }


    /* update */
    public void update(Graphics g) {
        paint(g);
    }

    /* paint */
     public void paint(Graphics g) {
        g.drawImage(myImages[currentImgNumber],20,20,this);
     }



0
Comment
Question by:mug
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
garik earned 100 total points
Comment Utility
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].flush();
  myImages[i] = getImage(getDocumentBase(), fullImgName);
  myTracker.addImage(myImages[i],i);
}
System.gc();
...

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.

0
 

Author Comment

by:mug
Comment Utility
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"
0
 

Expert Comment

by:narayan052997
Comment Utility
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.
0
 

Author Comment

by:mug
Comment Utility
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now