Link to home
Start Free TrialLog in
Avatar of sybe
sybe

asked on

How to destroy applet when browser leaves document

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.


ASKER CERTIFIED SOLUTION
Avatar of russgold
russgold

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sybe
sybe

ASKER

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.
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?

Avatar of sybe

ASKER

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";

      start();
      return true;      
}


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

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) { }
      }
      stop();
}

public void stop() {
      if (workThread != null) {
            workThread.stop();
      }
}

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.