Solved

Animation: *.gifs flood memory

Posted on 1997-06-12
4
1,995 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cipher Configuration on Apache HTTPD 4 62
Spring Framework HTTPSession management 1 33
nextBoolean(double p) for Random class 3 40
web services creation SOAP vs REST 5 37
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 last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

816 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

12 Experts available now in Live!

Get 1:1 Help Now