Solved

How to destroy applet when browser leaves document

Posted on 1997-08-17
5
876 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
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.
0
 
LVL 28

Author Comment

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

Expert Comment

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

0
 
LVL 28

Author Comment

by:sybe
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";

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

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

696 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