[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Animation: *.gifs flood memory

Posted on 1997-06-12
4
Medium Priority
?
2,001 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 300 total points
ID: 1221606
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
ID: 1221607
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
ID: 1221608
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
ID: 1221609
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 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