TOC PREV NEXT INDEX




Recovering Memory


Some systems require that the JVM stay in memory and be running at all times. This is typically true for systems where all the software, including the system GUI, is written in Java. The SavaJe operating system is an example, as are a number of small embedded devices.

Take extra care when checking for memory leaks in such an environment. It is not enough to look at the system memory before and after your application has been run. You must also consider the disposal of the shell and JVM support for unloading of classes.

Disposing of the Application Shell

If you have created a GUI wrapper around the HTML component, you need to unload the wrapper. If the wrapper is not garbage collected and has references to ICEreader classes (usually StormBase), some memory will not be unloaded.

You also need to dispose of StormBase objects you have created, as well as any supporting objects created by the wrapper. This could be, for example, a Tooltip object or a HistoryManager object.

The following is an example:

import ice.pilots.html4.*;

import ice.storm.*;

import java.awt.*;

import java.awt.event.*;
 
public class MyBrowser {
 
    public static void main(String[] args) {

        StormBase base = new StormBase();

        String loc = "http://www.yahoo.com/";

        if(args.length > 0) {

            loc=args[0];

        }
 
        base.renderContent(loc,null,"MyBrowser");

    }

}
 
class MyCallback implements ViewportCallback {
 
    private StormBase base;
 
    public void init(StormBase base) {

        this.base=base;
 
    }
 
    public Container createTopLevelContainer(Viewport viewport) {

        BrowserFrame f = new BrowserFrame(base,viewport);

        f.setVisible(true);

        return f.getPanel();

    }
 
    public void disposeTopLevelContainer(Viewport viewport) {}
 
    public void processViewportMessage

        (Viewport view, String messageName, Object addArg, Object value)

    {

    //do nothing

    }
 
}
 
class BrowserFrame extends Frame implements WindowListener{
 
    private Panel panel;

    private StormBase base;
 
    public BrowserFrame(StormBase stormBase,Viewport viewport) {

        addWindowListener(this);

        base=stormBase;

        panel = new Panel();

        add(panel,"Center");

        setSize(600,600);

    }
 
    public Panel getPanel() {

        return panel;

    }
 
    public void windowClosing(WindowEvent e) {

        //Remember to dispose the stormbase.

            base.dispose();

            base = null;

            dispose();

    }
 
    public void windowClosed(WindowEvent ev) {}

    public void windowOpened(WindowEvent ev) {}

    public void windowIconified(WindowEvent ev) {}

    public void windowDeiconified(WindowEvent ev) {}

    public void windowActivated(WindowEvent ev) {}

    public void windowDeactivated(WindowEvent ev) {}

}
 
Determining if the JVM Supports Unloading of Classes

If the JVM does not support unloading of classes, you lose the memory involved in storing each class, and the memory cannot be retrieved until the JVM is restarted.

On a typical system, this could be about 800 K, depending on the configuration of the application and how much memory the particular JVM requires for storing class information.

For more information, see http://java.sun.com/docs/books/jls/unloading-rationale.html



Copyright 2005. ICEsoft Technologies, Inc.
http://www.icesoft.com

TOC PREV NEXT INDEX