How to destroy applet when browser leaves document

Posted on 1997-08-17
Last Modified: 2008-03-10
I have made an applet which works fine. Except that when the same page is called in another browser window, the applet interferes with the applet in the original window. This happens even when the original window is closed, or now contains a different document without the applet.

I have checked some things, and the main thing I found out was that the applet is not destroyed when the browser leaves the document. It remains in memory !!

My question is how do i make the browser destroy the applet when the browser leaves the document with the applet.

I suspect that the browser keeps all the applet in memory  as long as the applets are not explicitely destroyed. This might explain memory problems after visiting a couple of pages with heavy java-applets.

Question by:sybe
  • 3
  • 2

Accepted Solution

russgold earned 100 total points
ID: 1225190
First of all, your suspicion is correct. Applets are not destroyed when the browser leaves the document with the applet. This allows you to return to that applet later by simply hitting the 'back' button.

It also means that hidden applets can continue to run, sucking up CPU cycles.

There is a standard way to address this problem:  When the browser leaves your applet's page, it calls the 'stop()' method of your applet. You should override this method to kill any active threads, to prevent them from running while your applet is hidden.

If you want, you can also override the 'start()' method to resume any killed threads. It will be called when your applet starts, and again if the browser ever returns to your applet's page.
LVL 28

Author Comment

ID: 1225191
I do understand your suggestion, but it does not solve the problem in my case, because in my applet I use a thread which is stopped and started depending on the "state" of the applet. That means I already have the start() and a stop() overriden.

The best way would be to check how the stop() method is called: if it is from the applet itself, then don't destroy the applet, if it is from the browser then do destroy the applet.

Is it possible to pass arguments to the stop() method ?
and does the browser pass any arguments ?

For the time being I leave the question open, though your answer gave me some ideas to try on.

Expert Comment

ID: 1225192
Then perhaps some clarification is in order. You described two problems: 1. New applets interfere with old, and 2. Applet memory is not reclaimed.

With regard to the second: Browsers have total control over when to reclaim memory from applets. Currently, most do not - ever.  You cannot really control that, since only the browser knows whether your applet will ever be restarted.

As to the first: if you are really stopping your thread in your Applet's 'stop()' method, there should be no interference  (Please distinguish between the Applet method and the similarly named method on a Thread).  Your comment that you need to tell when the *browser* calls the method and the fact that you are seeing interference makes me think otherwise.

What exactly do you mean by the Applet's "state" - and how do you detect changes in it to kill threads?

LVL 28

Author Comment

ID: 1225193
Below is the part of the code that i talk about. A mouse events triggers a new thread, that keeps on going as long as no other event interferes. When that happens, the stop() is called and the thread is killed.

When the browser leaves the page, the stop() method is called too, which is ok, but i would like to destroy the applet in that situation.

If you state that it is impossible to write code that does that, i will believe you, and consider my question as answered.

public boolean mouseUp(java.awt.Event evt, int x, int y) {

      -- do something --

      Situation = "on";

      return true;      

public void start() {
      workThread = new Thread(this);

public void run() {
      while (Situation == "on") {
            -- get a variable from somewhere --
                  if ( -- variable == something) {
                              do something
                              Situation = "off";
            try {Thread.sleep(SleepStaticRead);}
            catch (InterruptedException e) { }

public void stop() {
      if (workThread != null) {


Expert Comment

ID: 1225194
1. Java garbage collection ensures that objects will not be destroyed as long as *any* references to them exist. Since the browser holds a reference to your applet, it will prevent it from being destroyed until *it* wants it to be. As long as you kill your threads appropriately, it should not matter.

2. It does appear that you are killing your threads when the browser leaves the applet, so I don't really understand your comment about applets interfering with each other.

3. If you do want to take different actions on applet exit from your program determining that a thread should stop, you can make a pair of new methods which control the thread and call them from both the stop/start methods and your mouseUp/run methods.  start/stop can then take additional steps.

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle 11g 23 80
tomcat not starting 6 45
java stored proc example 9 29
Adding multiple JVM environments to RedHat 6 7 20
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)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

770 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