001: /*
002: * HeapMemoryPanel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.Dimension;
025: import java.awt.GridBagConstraints;
026: import java.awt.GridBagLayout;
027: import java.awt.Insets;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030:
031: import javax.swing.BorderFactory;
032: import javax.swing.DefaultBoundedRangeModel;
033: import javax.swing.JButton;
034: import javax.swing.JLabel;
035: import javax.swing.JPanel;
036: import javax.swing.JProgressBar;
037:
038: /* ----------------------------------------------------------
039: * CVS NOTE: Changes to the CVS repository prior to the
040: * release of version 3.0.0beta1 has meant a
041: * resetting of CVS revision numbers.
042: * ----------------------------------------------------------
043: */
044:
045: /**
046: *
047: * @author Takis Diakoumis
048: * @version $Revision: 1.5 $
049: * @date $Date: 2006/05/14 06:56:07 $
050: */
051: public class HeapMemoryPanel extends JPanel implements ActionListener {
052:
053: /** timer object for heap display */
054: private java.util.Timer progTimer;
055:
056: /** progress bar model */
057: private ProgressModel progModel;
058:
059: /** the progress bar */
060: private JProgressBar memProgress;
061:
062: /** Indicates the timer has started */
063: private boolean timerStarted;
064:
065: public HeapMemoryPanel() {
066: super (new GridBagLayout());
067:
068: try {
069: jbInit();
070: } catch (Exception e) {
071: e.printStackTrace();
072: }
073:
074: }
075:
076: private void jbInit() {
077: JPanel base = new JPanel(new GridBagLayout());
078:
079: JLabel line1 = new JLabel("Measures the size of the");
080: JLabel line2 = new JLabel(
081: "Java Virtual Machine\'s object heap.");
082:
083: progModel = new ProgressModel();
084: memProgress = new JProgressBar(progModel);
085: memProgress.setPreferredSize(new Dimension(265, 25));
086:
087: JButton gcButton = new JButton("Run Garbage Collector");
088: gcButton.addActionListener(this );
089:
090: base.setBorder(BorderFactory.createEtchedBorder());
091:
092: GridBagConstraints gbc = new GridBagConstraints();
093:
094: Insets ins = new Insets(0, 5, 3, 5);
095: gbc.gridx = 0;
096: gbc.gridy = 0;
097: gbc.insets = ins;
098: base.add(line1, gbc);
099: gbc.insets = ins;
100: gbc.gridy++;
101: base.add(line2, gbc);
102: gbc.gridy++;
103: gbc.insets.top = 10;
104: gbc.insets.left = 0;
105: gbc.insets.right = 0;
106: gbc.insets.bottom = 10;
107: gbc.fill = GridBagConstraints.BOTH;
108: base.add(memProgress, gbc);
109: gbc.gridy++;
110: gbc.insets.top = 5;
111: gbc.insets.left = 0;
112: gbc.insets.right = 0;
113: gbc.insets.bottom = 0;
114: gbc.ipady = 5;
115: gbc.fill = GridBagConstraints.NONE;
116: base.add(gcButton, gbc);
117:
118: startMeasure(progModel, memProgress);
119:
120: setPreferredSize(new Dimension(339, 168));
121: add(base, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0,
122: GridBagConstraints.SOUTHEAST, GridBagConstraints.BOTH,
123: new Insets(3, 3, 3, 3), 0, 0));
124: }
125:
126: public void actionPerformed(ActionEvent e) {
127: int total = (int) Runtime.getRuntime().totalMemory();
128: int free = (int) Runtime.getRuntime().freeMemory();
129: int totalUsedBefore = total - free;
130:
131: System.gc();
132:
133: total = (int) Runtime.getRuntime().totalMemory();
134: free = (int) Runtime.getRuntime().freeMemory();
135: int totalUserAfter = total - free;
136:
137: System.err.println("Garbage collection realeased "
138: + ((totalUsedBefore - totalUserAfter) / 1000) + "Kb.");
139: }
140:
141: /**
142: * Stops the timer controlling the heap bar.
143: */
144: public void stopTimer() {
145: if (progTimer != null) {
146: progTimer.cancel();
147: }
148: timerStarted = false;
149: progTimer = null;
150: }
151:
152: /**
153: * Starts the timer controlling the heap bar.
154: */
155: public void startTimer() {
156: if (!timerStarted) {
157: if (progTimer != null) {
158: startMeasure(progModel, memProgress);
159: }
160: }
161: }
162:
163: private void startMeasure(final ProgressModel progModel,
164: final JProgressBar memProgress) {
165: memProgress.setStringPainted(true);
166: final String used_s = " Kb used, ";
167: final String total_s = " Kb total";
168: final int thou = 1000;
169:
170: final Runnable showProgress = new Runnable() {
171: public void run() {
172: int total = (int) Runtime.getRuntime().totalMemory();
173: int free = (int) Runtime.getRuntime().freeMemory();
174: int used = total - free;
175: progModel.setMaximum(total);
176: progModel.setValue(used);
177: memProgress.setString((used / thou) + used_s
178: + (total / thou) + total_s);
179: }
180: };
181:
182: java.util.TimerTask updateProgress = new java.util.TimerTask() {
183: public void run() {
184: java.awt.EventQueue.invokeLater(showProgress);
185: }
186: };
187: progTimer = new java.util.Timer();
188: progTimer.schedule(updateProgress, 0, 1500);
189: timerStarted = true;
190: }
191:
192: static class ProgressModel extends DefaultBoundedRangeModel {
193:
194: private int max;
195: private int min;
196: private int value;
197:
198: public ProgressModel() {
199: }
200:
201: public int getMaximum() {
202: return max;
203: }
204:
205: public int getMinimum() {
206: return 0;
207: }
208:
209: public int getValue() {
210: return getMaximum()
211: - (int) Runtime.getRuntime().freeMemory();
212: }
213:
214: public void setMaximum(int i) {
215: max = i;
216: }
217:
218: public void setMinimum(int i) {
219: min = 0;
220: }
221:
222: public void setValue(int i) {
223: value = i;
224: fireStateChanged();
225: }
226:
227: } // ProgressModel
228:
229: }
|