1 24 25 package org.objectweb.cjdbc.scenario.tools.util; 26 27 import java.awt.GridLayout ; 28 import java.awt.event.KeyEvent ; 29 import java.awt.event.KeyListener ; 30 import java.awt.event.WindowEvent ; 31 import java.awt.event.WindowListener ; 32 33 import javax.swing.JFrame ; 34 import javax.swing.JLabel ; 35 import javax.swing.JTextField ; 36 37 43 public class MemoryProfiler extends Thread 44 implements 45 WindowListener , 46 KeyListener 47 { 48 49 boolean useFrame = false; 50 boolean quit = false; 51 52 JFrame frame; 53 JTextField freeMemory; 54 JTextField minMemory; 55 JTextField timeRunning; 56 long started; 57 58 String frees = ""; 59 long free = 0; 60 long min = 0; 61 long timeout = 1000; 62 private long time; 63 private String stime; 64 private JTextField refreshRate; 65 private long avg; 66 private long tot; 67 private int count; 68 69 public void quit() 70 { 71 this.quit = true; 72 } 73 74 77 public void keyPressed(KeyEvent e) 78 { 79 81 } 82 83 86 public void keyReleased(KeyEvent e) 87 { 88 90 } 91 92 95 public void keyTyped(KeyEvent e) 96 { 97 String s = refreshRate.getText(); 98 timeout = Long.parseLong(s) * 1000; 99 } 100 101 104 public void windowActivated(WindowEvent e) 105 { 106 108 } 109 110 113 public void windowClosed(WindowEvent e) 114 { 115 this.useFrame = false; 116 117 } 118 119 122 public void windowClosing(WindowEvent e) 123 { 124 this.useFrame = false; 125 126 } 127 128 131 public void windowDeactivated(WindowEvent e) 132 { 133 135 } 136 137 140 public void windowDeiconified(WindowEvent e) 141 { 142 144 } 145 146 149 public void windowIconified(WindowEvent e) 150 { 151 153 } 154 155 158 public void windowOpened(WindowEvent e) 159 { 160 162 } 163 164 167 public MemoryProfiler() 168 { 169 if (useFrame) 170 { 171 frame = new JFrame ("Values"); 172 GridLayout gridLayout = new GridLayout (4, 2); 173 frame.getContentPane().setLayout(gridLayout); 174 freeMemory = new JTextField (); 175 freeMemory.setEditable(false); 176 minMemory = new JTextField (); 177 minMemory.setEditable(false); 178 timeRunning = new JTextField (); 179 timeRunning.setEditable(false); 180 refreshRate = new JTextField (String.valueOf(timeout / 1000)); 181 refreshRate.addKeyListener(this); 182 frame.getContentPane().add(new JLabel ("Free Memory")); 183 frame.getContentPane().add(freeMemory); 184 frame.getContentPane().add(new JLabel ("Min Free Mem")); 185 frame.getContentPane().add(minMemory); 186 frame.getContentPane().add(new JLabel ("Time Running")); 187 frame.getContentPane().add(timeRunning); 188 frame.getContentPane().add(new JLabel ("Refresh Rate (in sec)")); 189 frame.getContentPane().add(refreshRate); 190 frame.setSize(200, 100); 191 frame.pack(); 192 frame.validate(); 193 frame.setVisible(true); 194 frame.addWindowListener(this); 195 } 196 } 197 198 public void run() 199 { 200 started = System.currentTimeMillis(); 201 while (!quit) 202 { 203 synchronized (this) 204 { 205 try 206 { 207 wait(timeout); 208 } 209 catch (InterruptedException e) 210 { 211 e.printStackTrace(); 212 } 213 updateMemoryValue(); 214 } 215 } 216 } 217 218 private void updateMemoryValue() 219 { 220 free = (Runtime.getRuntime().freeMemory() / 1024); 221 tot += free; 222 count += 1; 223 time = System.currentTimeMillis() - started; 224 stime = String.valueOf(time / 1000); 225 frees = String.valueOf(free); 226 avg = tot / count; 227 if (min == 0 || min > free) 228 min = free; 229 230 if (useFrame) 231 { 232 freeMemory.setText(frees); 233 minMemory.setText(String.valueOf(min)); 234 timeRunning.setText(stime); 235 frame.getContentPane().repaint(); 236 } 237 else 238 System.out.println("Memory: FREE " + free + " ko, MIN " + min 239 + " ko, AVG " + avg + ". Running time is:" + stime + " s."); 240 } 241 242 } | Popular Tags |