This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Applets and the fly likes How to manage applet life-cycle? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Applets
Reply 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: 35443
    
    9
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.


Android appsImageJ pluginsJava web charts
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to manage applet life-cycle?
 
Similar Threads
JApplet UI and threads
windows user name
Applets alignment. cache & POST
Need help repainting a panel inside a JApplet
Reg:Applet to servlet Communication