Click to See Complete Forum and Search --> : Java SWING is tougher than you might think


nanode
02-08-2001, 11:23 AM
The other day I was presented with a challenge from someone who clearly wasn't a java fan, in particular, he hated SWING.

His claim was that creating new windows populated with widgets, then disposing them would ultimately cause massive memory leakage.

Below is some test code. Please excuse my arg. handling, I rarely write CLI apps in Java. :(

Running this should be self explanatory. Just be sure to add 'd' as the last arg if you want the windows to dispose.

So far, on win2k w/ jdk1.3 I created and disposed over 300,000 widgets and only lost about 2MB of RAM. I got twice that far on my Linux box (which had 1/2 the RAM as my work box).


import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class CrashMe {

public static void main(String[] args) {
int n = 0;
boolean dispose = false;
if (args.length > 0) {
n = Integer.valueOf(args[0]).intValue();
if (args.length == 2) {
if (args[1].equals("d")) { dispose = true; }
}
}
else {
System.out.println("Usage: java CrashMe [# instances] <d>");
System.exit(0);
}
for (int frames=0;frames<n;frames++) {
JPanel demo = new JPanel();
demo.setBackground(Color.white);
for (int i=0;i<n;i++) {
demo.add(new JButton("JButton #" + i));
}
JFrame f = new JFrame("Window #" + frames);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
f.getContentPane().add("Center", demo);
f.pack();
f.setSize(new Dimension(600,500));
f.show();
if (dispose) {
f.dispose();
}
}
System.out.println("Haha, I didn't crash");
}
}

kmj
02-08-2001, 11:27 AM
only 2MB?

and here I was thinking swing was the answer to all my mfc nightmares.

nanode
02-08-2001, 11:32 AM
KMJ:

dude, if you run that prog, it draws n JFrames each with n JButtons on each one.

Feel free to bring up this limitation at your next design meeting where you are asked to draw 50,000 widgets on the screen at once :)

Seriously, after an hour of running, is 2MB really bad? I'm used to slop, so I'm subjective. :)

jemfinch
02-08-2001, 11:56 AM
How do you decide if memory is lost or not?

Jeremy

nanode
02-08-2001, 01:35 PM
Jemfinch:

Suppose I rendered 100 widgets * 1Byte each.
So that's 100 Bytes for the whole deal.

When you dispose of a widget, the memory it occupied *should* be freed up automagically (you may chuckle).

If the avail. memory is less after all widgets are disposed of compared to before you rendered them, that means (to me) I didn't get back all the memory I should have.

lame code:

if (memory_before_widget < memory_after_widget_disposal) {
lost_memory = true;
}

I need some sleep :p

shortfella
02-08-2001, 05:10 PM
Please tell us how you check this!

My linux box can have up to 80MB free, I then run a program and then can have only 10MB free. Then I remember memory is cached and buffered.

X also may be taking some extra ram, I was told this wasn't a leak but there is some technical reason for it.

As for windows - that's anyones guess.

nanode
02-08-2001, 06:27 PM
I can say with any certainty how much memory is consumed. I just did ps aux
:(

There's probably some tool out there that'd be more scientific.