KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > util > MemoryProfiler


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.util;
26
27 import java.awt.GridLayout JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.awt.event.KeyListener JavaDoc;
30 import java.awt.event.WindowEvent JavaDoc;
31 import java.awt.event.WindowListener JavaDoc;
32
33 import javax.swing.JFrame JavaDoc;
34 import javax.swing.JLabel JavaDoc;
35 import javax.swing.JTextField JavaDoc;
36
37 /**
38  * This class defines a MemoryProfiler
39  *
40  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
41  * @version 1.0
42  */

43 public class MemoryProfiler extends Thread JavaDoc
44     implements
45       WindowListener JavaDoc,
46       KeyListener JavaDoc
47 {
48
49   boolean useFrame = false;
50   boolean quit = false;
51
52   JFrame JavaDoc frame;
53   JTextField JavaDoc freeMemory;
54   JTextField JavaDoc minMemory;
55   JTextField JavaDoc timeRunning;
56   long started;
57
58   String JavaDoc frees = "";
59   long free = 0;
60   long min = 0;
61   long timeout = 1000;
62   private long time;
63   private String JavaDoc stime;
64   private JTextField JavaDoc 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   /**
75    * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
76    */

77   public void keyPressed(KeyEvent JavaDoc e)
78   {
79     // TODO Auto-generated method stub
80

81   }
82
83   /**
84    * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
85    */

86   public void keyReleased(KeyEvent JavaDoc e)
87   {
88     // TODO Auto-generated method stub
89

90   }
91
92   /**
93    * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
94    */

95   public void keyTyped(KeyEvent JavaDoc e)
96   {
97     String JavaDoc s = refreshRate.getText();
98     timeout = Long.parseLong(s) * 1000;
99   }
100
101   /**
102    * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
103    */

104   public void windowActivated(WindowEvent JavaDoc e)
105   {
106     // TODO Auto-generated method stub
107

108   }
109
110   /**
111    * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
112    */

113   public void windowClosed(WindowEvent JavaDoc e)
114   {
115     this.useFrame = false;
116
117   }
118
119   /**
120    * @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
121    */

122   public void windowClosing(WindowEvent JavaDoc e)
123   {
124     this.useFrame = false;
125
126   }
127
128   /**
129    * @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
130    */

131   public void windowDeactivated(WindowEvent JavaDoc e)
132   {
133     // TODO Auto-generated method stub
134

135   }
136
137   /**
138    * @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
139    */

140   public void windowDeiconified(WindowEvent JavaDoc e)
141   {
142     // TODO Auto-generated method stub
143

144   }
145
146   /**
147    * @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
148    */

149   public void windowIconified(WindowEvent JavaDoc e)
150   {
151     // TODO Auto-generated method stub
152

153   }
154
155   /**
156    * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
157    */

158   public void windowOpened(WindowEvent JavaDoc e)
159   {
160     // TODO Auto-generated method stub
161

162   }
163
164   /**
165    * Creates a new <code>MemoryProfiler</code> object
166    */

167   public MemoryProfiler()
168   {
169     if (useFrame)
170     {
171       frame = new JFrame JavaDoc("Values");
172       GridLayout JavaDoc gridLayout = new GridLayout JavaDoc(4, 2);
173       frame.getContentPane().setLayout(gridLayout);
174       freeMemory = new JTextField JavaDoc();
175       freeMemory.setEditable(false);
176       minMemory = new JTextField JavaDoc();
177       minMemory.setEditable(false);
178       timeRunning = new JTextField JavaDoc();
179       timeRunning.setEditable(false);
180       refreshRate = new JTextField JavaDoc(String.valueOf(timeout / 1000));
181       refreshRate.addKeyListener(this);
182       frame.getContentPane().add(new JLabel JavaDoc("Free Memory"));
183       frame.getContentPane().add(freeMemory);
184       frame.getContentPane().add(new JLabel JavaDoc("Min Free Mem"));
185       frame.getContentPane().add(minMemory);
186       frame.getContentPane().add(new JLabel JavaDoc("Time Running"));
187       frame.getContentPane().add(timeRunning);
188       frame.getContentPane().add(new JLabel JavaDoc("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 JavaDoc 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