Solved

How to destroy applet when browser leaves document

Posted on 1997-08-17
5
851 Views
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.


0
Comment
Question by:sybe
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
russgold earned 100 total points
Comment Utility
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.
0
 
LVL 28

Author Comment

by:sybe
Comment Utility
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.
0
 
LVL 4

Expert Comment

by:russgold
Comment Utility
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?

0
 
LVL 28

Author Comment

by:sybe
Comment Utility
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();
      }
}

0
 
LVL 4

Expert Comment

by:russgold
Comment Utility
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.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now