package test;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
/**
* @author boesi
* @date 29.11.2008
*
*/
public class MemoryTester extends JFrame {
JButton btnStart = new JButton("Start");
JButton btnClear = new JButton("Leeren");
JButton btnGC = new JButton("Garbage Collector");
JButton btnRefresh = new JButton("Aktualisiern");
Runtime rt = Runtime.getRuntime();
Background bg = null;
ArrayList<Integer> array = new ArrayList<Integer>();
long start = 0;
JLabel labSz = new JLabel("", JLabel.RIGHT);
JLabel labDauer = new JLabel("", JLabel.RIGHT);
JLabel labFree = new JLabel("", JLabel.RIGHT);
JLabel labTotal = new JLabel("", JLabel.RIGHT);
JLabel labMax = new JLabel("", JLabel.RIGHT);
public void setMemText() {
synchronized (array) {
labSz.setText(String.format("%,d ", array.size()));
}
long dauer = (System.currentTimeMillis() - start) / 1000;
long hour = dauer / 3600;
long minute = (dauer % 3600) / 60;
long second = dauer - hour * 3600 - minute * 60;
labDauer.setText(String.format("%02d:%02d:%02d", hour, minute, second));
labFree.setText(String.format("%,d Byte", rt.freeMemory()));
labTotal.setText(String.format("%,d Byte", rt.totalMemory()));
labMax .setText(String.format("%,d Byte", rt.maxMemory()));
}
class Background extends Thread {
private boolean isRunning = false;
private boolean shouldStop = false;
public Background() {
btnStart.setText("Stop");
this.start();
}
protected void publish() {
SwingUtilities.invokeLater(new Thread() {
public void run() {
setMemText();
}
});
}
@Override
public void run() {
System.out.printf("Background Thread gestartet\n");
this.shouldStop = false;
this.isRunning = true;
start = System.currentTimeMillis();
Thread.currentThread().setName("Background");
int cnt = 0;
try {
while (! this.shouldStop) {
synchronized (array) {
array.add(array.size());
}
if ((cnt++ % 3000) == 0) {
this.publish();
Thread.yield();
}
}
} catch (OutOfMemoryError exc) {
System.out.printf("Houston ... \n");
exc.printStackTrace();
SwingUtilities.invokeLater(new Thread() {
public void run() {
kill();
}
});
}
this.isRunning = false;
System.out.printf("Background Thread beendet\n");
}
public void kill() {
btnStart.setText("Start");
this.shouldStop = true;
while (this.isRunning) Thread.yield();
bg = null;
}
};
ActionListener evtBtnStart = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (bg != null) bg.kill();
else bg = new Background();
}
};
ActionListener evtBtnClear = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
synchronized (array) {
array.clear();
array.trimToSize();
labSz.setText(String.valueOf(array.size()));
}
setMemText();
}
};
ActionListener evtBtnGC = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
rt.gc();
setMemText();
}
};
ActionListener evtBtnRefresh = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setMemText();
}
};
public MemoryTester() {
this.setLayout(new GridLayout(7, 2));
this.add(new JLabel("Größe ArrayList: ")); this.add(labSz);
this.add(new JLabel("Dauer: ")); this.add(labDauer);
this.add(new JLabel("Free Mem: ")); this.add(labFree);
this.add(new JLabel("Total Mem: ")); this.add(labTotal);
this.add(new JLabel("Max Mem: ")); this.add(labMax);
this.add(btnStart);
this.add(btnClear);
this.add(btnGC);
this.add(btnRefresh);
btnStart.addActionListener(evtBtnStart);
btnClear.addActionListener(evtBtnClear);
btnGC.addActionListener(evtBtnGC);
btnGC.addActionListener(evtBtnRefresh);
this.labSz.setText(String.valueOf(array.size()));
this.setMemText();
this.setTitle("Memory Tester");
this.setSize(new Dimension(250, 200));
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MemoryTester();
}
}