here is a simple program i use to launch xterms/ssh on the machines
from our network.
it is a program that reads a property file that looks a lot like the
one from xbuffy and displays as many buttons you specified with the
foreground/background color you chose and the command executed when
clicked.
i catch mouseEvents to paint the border of the buttons when the mouse
is over them, that looks like a roll-over button.
here is my problem: if i mouve the mouse around fast and a lot, the
memory use grows damn fast and i reach 22 megs used by that damn small
thing (xbuffy with much more code takes not even 1meg).
then at 22 it hangs...
so i decided to put a thread that would look every ten seconds for
events generated and lauch a System.gc() just to see if the memory was
taken back.
well NO !!!
test it yourself:
lauch the app, launch 'top' ordered my memory usage ('M') and look at
the memory use:
move the mouse over a button and out.
the memory grows slightly, then my GC thread goes doing stuff (which
takes memory, normal...) and 'garbage-collects'...
then some memory used by the GC thread is recovered but you'll see
some missing during the process.
only a little bit, but this is only for TWO MOUSE EVENTS AND A GC !!!
so imagine what it could mean for a real app...
now roll your mouse over the buttons during 10 minutes and you'll see
the meory disaster happen.
now get rid of the GC thread and you can only play with rolling your
mouse during one or two minutes.
can anybody tell me if this is normal behaviour ?
--
~~~~~~~~~~~~
~ ~ This morning, i found representatives of our business
~ Stef ~ model finishing my meal and visiting my turtle. Looks
~ the ~ like it means we're becoming aggressive.
~ i n ~
~ n a ~
~ f i ~
~ ormagic ~
~ ~
~~~~~~~~~~~~
These are only some stupid signatures, i never said you had to read
them...
package util;
import javax.swing.border.LineBorder;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.lang.Runtime;
import java.lang.String;
import java.lang.Math;
import java.lang.Integer;
import java.util.StringTokenizer;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
import java.util.Locale;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Hashtable;
import java.util.Vector;
import java.awt.Color;
public class XTerms extends JFrame{
private static ResourceBundle resources;
boolean mouseEvents=false;
static {
try {
resources = ResourceBundle.getBundle("XTerms",
Locale.getDefault());
} catch (MissingResourceException mre) {
System.err.println("XTerms.properties not found");
System.exit(1);
}
}
void getAttr(String[] args){
String[] attrs={"geometry"};
for(int i=0;i<args.length;i++){
int c=0;
for(c=0;c<attrs.length ;c++){
if(args[i].equals("-"+attrs[c])){
if(++i <args.length){
attributes.put(attrs[c],args[i]);
break;
}else
attError();
}
}
if(c==attrs.length){
if(args[i].equals("--help"))
attHelp();
else
attError();
}
}
}
void attError(){
System.err.println("usage: XTerms [-geometry -/+X-/+X]");
System.exit(0);
}
void attHelp(){
System.out.println("usage: XTerms [-geometry -/+X-/+X]");
System.exit(0);
}
Point getLoc(String str){
// syntax is (-|+)X(-|+)X where X is a number
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
int index=str.lastIndexOf('+');
int index2=str.lastIndexOf('-');
index=Math.max(index,index2);
int x=Integer.parseInt(str.substring(1,index));
if(str.charAt(0)=='-'){
x=d.width-getSize().width-x;
}
int y=Integer.parseInt(str.substring(index+1,str.length()));
if(str.charAt(index)=='-'){
y=d.height-getSize().height-y;
}
return new Point(x,y);
}
protected String getResourceString(String nm) {
String str;
try {
str = resources.getString(nm);
} catch (MissingResourceException mre) {
System.err.println("(missing ressource..."+nm+") ");
str = null;
}
return str;
}
protected String[] tokenize(String input) {
Vector v = new Vector();
StringTokenizer t = new StringTokenizer(input);
String cmd[];
while (t.hasMoreTokens())
v.addElement(t.nextToken());
cmd = new String[v.size()];
for (int i = 0; i < cmd.length; i++)
cmd[i] = (String) v.elementAt(i);
return cmd;
}
public XTerms(String[] args){
super("choose an xterm");
addWindowListener(new AppCloser());
getAttr(args);
String[] buttonNames=tokenize(getResourceString("ButtonNames"));
getContentPane().setLayout(new GridLayout(buttonNames.length,1));
for(int i=0;i<buttonNames.length;i++){
JButton button=new JButton(buttonNames[i]);
button.addActionListener(new TermLauncher(getResourceString(buttonNames[i]+"Command").trim()));
button.setForeground(stringToColor(getResourceString(buttonNames[i]+"FG").trim()));
button.setBackground(stringToColor(getResourceString(buttonNames[i]+"BG").trim()));
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setRolloverEnabled(true);
button.setBorder(new LineBorder(stringToColor(getResourceString(buttonNames[i]+"FG").trim())));
button.addMouseListener(new RolloverPainter(button));
getContentPane().add(button);
}
// setSize(100,300);
pack();
if(attributes.containsKey("geometry"))
setLocation(getLoc((String)attributes.get("geometry")));
(new GarbageThread()).start();
}
class GarbageThread extends Thread{
public void run(){
while(true){
try{
sleep(10000);
}catch(InterruptedException xxx){
xxx.printStackTrace();
}
if(mouseEvents){
System.runFinalization();
System.gc();
mouseEvents=false;
}
}
}
}
class RolloverPainter implements MouseListener{
JButton button;
public RolloverPainter(JButton button){
this.button=button;
}
public void mouseEntered(MouseEvent e){
mouseEvents=true;
button.setBorderPainted(true);
button.repaint();
}
public void mouseExited(MouseEvent e){
mouseEvents=true;
button.setBorderPainted(false);
button.repaint();
}
public void mouseClicked(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
}
class TermLauncher implements ActionListener{
String cmd;
public TermLauncher(String command){
cmd=command;
}
public void actionPerformed(ActionEvent e){
mouseEvents=true;
try{
Runtime.getRuntime().exec(cmd);
}catch(java.io.IOException ex){
System.err.println("exception in command exec");
ex.printStackTrace();
}
}
}
//listener to shut down
class AppCloser extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(1);
}
}
Hashtable attributes=new Hashtable();
public static void main(String[] args){
XTerms xterms=new XTerms(args);
xterms.show();
}
/**
* Convert a "#FFFFFF" hex string to a Color.
* If the color specification is bad, an attempt
* will be made to fix it up.
*/
static final Color hexToColor(String value) {
if (value.startsWith("#")) {
String digits = value.substring(1, Math.min(value.length(), 7));
String hstr = "0x" + digits;
Color c = Color.decode(hstr);
return c;
}
return null;
}
/**
* Convert a color string "RED" or "#NNNNNN" to a Color.
* Note: This will only convert the HTML3.2 colors strings
* or string of length 7
* otherwise, it will return null.
*/
public Color stringToColor(String str) {
Color color = null;
if (str.charAt(0) == '#')
color = hexToColor(str);
else if (str.equalsIgnoreCase("Black"))
color = hexToColor("#000000");
else if(str.equalsIgnoreCase("Silver"))
color = hexToColor("#C0C0C0");
else if(str.equalsIgnoreCase("Gray"))
color = hexToColor("#808080");
else if(str.equalsIgnoreCase("White"))
color = hexToColor("#FFFFFF");
else if(str.equalsIgnoreCase("Maroon"))
color = hexToColor("#800000");
else if(str.equalsIgnoreCase("Red"))
color = hexToColor("#FF0000");
else if(str.equalsIgnoreCase("Purple"))
color = hexToColor("#800080");
else if(str.equalsIgnoreCase("Fuchsia"))
color = hexToColor("#FF00FF");
else if(str.equalsIgnoreCase("Green"))
color = hexToColor("#008000");
else if(str.equalsIgnoreCase("Lime"))
color = hexToColor("#00FF00");
else if(str.equalsIgnoreCase("Olive"))
color = hexToColor("#808000");
else if(str.equalsIgnoreCase("Yellow"))
color = hexToColor("#FFFF00");
else if(str.equalsIgnoreCase("Navy"))
color = hexToColor("#000080");
else if(str.equalsIgnoreCase("Blue"))
color = hexToColor("#0000FF");
else if(str.equalsIgnoreCase("Teal"))
color = hexToColor("#008080");
else if(str.equalsIgnoreCase("Aqua"))
color = hexToColor("#00FFFF");
return color;
}
}
ButtonNames=machine1 machine2 machine3
machine1Command=nohup xterm -fg red -bg black -vb -sb -sl 1024 -T tangleterm -e ssh -X
machine1
machine1FG=red
machine1BG=black
machine2Command=nohup xterm -fg green -bg black -vb -sb -sl 1024 -T machine2term -e
ssh -X machine2
machine2FG=green
machine2BG=black
machine3Command=nohup xterm -fg #00ffff -bg black -vb -sb -sl 1024 -T machine3term -e
ssh -X machine3
machine3FG=#00ffff
machine3BG=black
machine4Command=nohup xterm -fg #00ffff -bg black -vb -sb -sl 1024 -T machine4term -e
ssh -X machine4
machine4FG=#00ffff
machine4BG=black
machine5Command=nohup xterm -fg #00ffff -bg black -vb -sb -sl 1024 -T machine5term -e
ssh -X machine5
machine5FG=#00ffff
machine5BG=black
machine6Command=nohup xterm -fg #00ffff -bg black -vb -sb -sl 1024 -T machine6term -e
ssh -X machine6
machine6FG=#00ffff
machine6BG=black
machine7Command=nohup xterm -fg #00ffff -bg black -vb -sb -sl 1024 -T machine7term -e
ssh -X machine7
machine7FG=#00ffff
machine7BG=black
machine8Command=nohup xterm -fg #00ffff -bg black -vb -sb -sl 1024 -T machine8term -e
ssh -X machine8
machine8FG=#00ffff
machine8BG=black
machine9Command=nohup xterm -fg #00ffff -bg black -vb -sb -sl 1024 -T machine9term -e
ssh -X machine9
machine9FG=#00ffff
machine9BG=black
machine10Command=nohup xterm -fg #00ffff -bg black -vb -sb -sl 1024 -T machine10term
-e ssh -X machine10
machine10FG=#00ffff
machine10BG=black