The following test case, which was taken from the JDC comments for bug
6257260: Memory leak on closing JFrame
shows a memory leak in XAWT in 5.0:
---- test begins -----
import java.awt.Button;
import java.awt.Event;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class XtoolkitIconLeak extends Frame {
static boolean S_allowIcon = true;
static int S_offset = 100;
boolean S_etype = true;
Button S_b = null;
byte[] S_bigDummyData = null;
public XtoolkitIconLeak(String name, boolean etype /* if true then exit app on windowClose() */){
S_etype = etype;
setTitle(name);
/*--------------------------------------------------------------
* Under Solaris 10 X86 if we intall an ICON in the frame, GC fails
* and we leak 5M per window iteration (e.g. new frame and close)
* -Dawt.toolkit=sun.awt.X11.XToolkit
* or
* unsetenv AWT_TOOLKIT
*/
Toolkit kit = Toolkit.getDefaultToolkit();
Image icon = kit.getImage("1.gif");
if (S_allowIcon) {
setIconImage(icon);
}
/*--------------------------------------------------------------------*/
if (S_etype == true) {
// only the primary window has a new frame button
setBounds(0,0,250,100);
setLayout(new GridLayout(1,1));
S_b = new Button("create new frame");
add(S_b);
}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we){
dispose();
doGC(4,"Frame.dispose()");
if (S_etype == true) {
// only the primary window invokes an application exits
System.exit(0);
}
}
});
setVisible(true);
}
public void doGC(int passes, String activity)
{
Runtime rt = Runtime.getRuntime();
for (int i = 0; i< passes; i++) {
System.gc();
rt.gc();
rt.runFinalization();
}
long free = rt.freeMemory();
long total = rt.totalMemory();
long used = total - free;
String sused = "" + used;
while (sused.length() < 9) sused = " " + sused ;
System.out.println("HEAP USED = " + sused + ", ACTIVITY = " + activity);
}
public boolean action(Event event, Object arg)
{
if (event.target.equals(S_b)) {
doGC(4,"new Frame()");
XtoolkitIconLeak popup = new XtoolkitIconLeak("new sub frame",false);
S_offset = S_offset + 22;
popup.setBounds(S_offset, S_offset, 250,100);
popup.S_bigDummyData = new byte[1000*1000*5];
}
return true;
}
public static void main(String args[]){
System.out.println("awt.toolkit="+System.getProperty("awt.toolkit"));
if (args.length > 0 && args[0].equalsIgnoreCase("NOICON")) {
XtoolkitIconLeak.S_allowIcon = false;
System.out.println("Not installing icons on frame via Frame.setIconImage()");
}
XtoolkitIconLeak Xtest = new XtoolkitIconLeak("main frame",true);
}
}
----- test ends -----
I can reproduce the leak on Solaris 10 with Xawt toolkit.
The bug is not reproducible on mustang.