It's not a secret anymore!*
The moose likes Applets and the fly likes How to manage applet life-cycle? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "How to manage applet life-cycle? " Watch "How to manage applet life-cycle? " New topic
Author

How to manage applet life-cycle?

Dan King
Ranch Hand

Joined: Mar 18, 2009
Posts: 84
I'd like to better manage the life-cycle of my applet. I want the applet to destroy the object created during initialization and release occupied memory, when the user navigates away from the page containing the applet.

Currently, when the user navigates away the applet continues to exist in spite of being set to null and the java.exe continues to consume memory. How can I achieve my goal of destroying the object and releasing memory? Below is my code:

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
For starters, you may want to get in the habit of adding "@Override" annotations to methods that are supposed to override other methods. Otherwise it's easy to mistype method names and not have them override superclass methods (like the "destory" method in your code). (Assuming you're using at least Java 5, of course.)

You can indicate to the JVM that it should do a garbage collection in the stop or destroy methods by calling System.gc(), but there's no guarantee that it's going to happen.

Is the memory consumption an actual problem, or is this just a problem you anticipate and are trying to head off? The JVM for sure will not terminate until the browser exits.


Ping & DNS - my free Android networking tools app
Dan King
Ranch Hand

Joined: Mar 18, 2009
Posts: 84
Ulf Dittmer wrote:For starters, you may want to get in the habit of adding "@Override" annotations to methods that are supposed to override other methods. Otherwise it's easy to mistype method names and not have them override superclass methods (like the "destory" method in your code). (Assuming you're using at least Java 5, of course.)

You can indicate to the JVM that it should do a garbage collection in the stop or destroy methods by calling System.gc(), but there's no guarantee that it's going to happen.

Is the memory consumption an actual problem, or is this just a problem you anticipate and are trying to head off? The JVM for sure will not terminate until the browser exits.


You're absolutely right, I should get in the habit of adding "@Override" annotations to methods that are supposed to be overridden. Clearly in this case, the annotation would have helped catch the mistyped method identifier.

As of now, memory consumption is not a problem, nor do I see it being a problem. I was simply tinkering with my code, trying to make my applet more efficient. Do you have any general suggestions/advice?
 
Don't get me started about those stupid light bulbs.
 
subject: How to manage applet life-cycle?