Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Animation: *.gifs flood memory

Posted on 1997-06-12
4
Medium Priority
?
2,000 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

636 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