jQuery in Action, 3rd edition
The moose likes Applets and the fly likes Applet memory usage - please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Applet memory usage - please help" Watch "Applet memory usage - please help" New topic

Applet memory usage - please help

Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Hi all,
I have an applet which allows a user to acquire images (via a remote server) and view them via the code:

Before each time I call the code above to view an image,
I first call:

to try to get the garbage collector to keep the memory as low as possible.
It seems to work fine. My question is about the memory used. I am using JDK 1.2.2 on a WindowsNT 4.0 system and Netscape 4.73. When I open the task manager to view the amount of memory used by my process, it goes up each time I view an image, up to a point, then it seems to level out. I have tried this using both Netscape and Internet Explorer on Windows systems and Netscape on a Unix system. The numbers vary slightly, but the action of increasing memory usage and then inexplicably leveling out seems to hold in all cases.
My question is, why does this occur? Is the garbage collector "smart" enough to sense when it needs to free up memory? Any insight would be greatly appreciated.
Thanks in advance.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
well don't take my word for it but this is what I seem to remember after reading some books. The garbage collector really isn't that smart although I'm sure it's been made better with every version of the JDK. You could for instance try to use the new "hotspot" VM that comes with the 1.3 JDK.
The garbage collector is pretty much like a daemon thread,working in the background and doing it's work,freeing up memory by getting rid of objects that aren't referenced anymore, while the processor is sitting idle for a split second. If the processor is working all the time there will not be any garbage collected because the gc has the lowest priority, so this will go on till the memory is full and the garbage just has to be collected or nothing will go anymore..at that point the garbage collector starts working no matter all the other processes with higher priorities.
There are situations though that stuff doesnt get gc'd, for instance if it's not explicitly made by the keyword "new". I think this could go for images that are painted on your screen. To free up the memory that these take up and get rid of the images you explicitly need to write a method for just that. C++ has like a "destructor" I read but Java doesn't so it's up to you to do that.
As far as i can tell there isn't much more you can do though than call System.gc . This you should do after all the objects are created and discarded though or else it won't call all the finalizers. Maybe you could also add another line after System.gc using System.runFinalization().
Good luck )
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Thanks for the input!
I will look into System.runFinalization().
I agree. Here's the link: http://aspose.com/file-tools
subject: Applet memory usage - please help
It's not a secret anymore!