* ======================================
* JFreeChart : a free Java chart library
* ======================================
*
* Project Info: <http://www.object-refinery.com/jfreechart/index.html>
* Project Lead: David Gilbert (david.gilbert@object-refinery.com);
*
* (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* ----------------
* MemoryUsage.java
* ----------------
* (C) Copyright 2002, 2003, by Object Refinery Limited and Contributors.
*
* Original Author: Tony Bianchini;
* Contributor(s): David Gilbert;
*
* $Id: MemoryUsage.java,v 1.4 2002/10/16 14:17:55 mungady Exp $
*
* Changes
* -------
* 10-Sep-2002 : Version 1, based on code by Tony Bianchini (DG);
* 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
* 16-Oct-2002 : Removed redundant attributes (DG);
* 18-Oct-2002 : Moved to com.jrefinery.chart.demo.premium package (DG);
* 25-Apr-2003 : Updated for JFreeChart 0.9.8, and moved to com.jrefinery.chart.demo package (DG);
*
*/
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.DefaultXYItemRenderer;
import org.jfree.chart.renderer.XYItemRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
/**
* A demo application showing a dynamically updated chart that displays the current JVM memory
* usage.
*
* @author Tony Bianchini
* @author David Gilbert
*/
public class MemoryUsage extends JPanel {
/** Time series for total memory used. */
private TimeSeries total;
/** Time series for free memory. */
private TimeSeries free;
/**
* Creates a new application.
*/
public MemoryUsage() {
super(new BorderLayout());
// create two series that automatically discard data more than 30 seconds old...
this.total = new TimeSeries("Total", Millisecond.class);
this.total.setHistoryCount(30000);
this.free = new TimeSeries("Free", Millisecond.class);
this.free.setHistoryCount(30000);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(total);
dataset.addSeries(free);
DateAxis domain = new DateAxis("Time");
NumberAxis range = new NumberAxis("Memory");
XYItemRenderer renderer = new DefaultXYItemRenderer();
renderer.setSeriesPaint(0, Color.red);
renderer.setSeriesPaint(1, Color.green);
renderer.setBaseStroke(
new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)
);
XYPlot xyplot = new XYPlot(dataset, domain, range, renderer);
xyplot.setBackgroundPaint(Color.black);
domain.setAutoRange(true);
domain.setLowerMargin(0.0);
domain.setUpperMargin(0.0);
domain.setTickLabelsVisible(true);
range.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
JFreeChart chart = new JFreeChart(
"Memory Usage",
JFreeChart.DEFAULT_TITLE_FONT,
xyplot,
true
);
ChartPanel chartPanel = new ChartPanel(chart);
add(chartPanel);
}
/**
* Adds an observation to the 'total memory' time series.
*
* @param y the total memory used.
*/
private void addTotalObservation(double y) {
total.add(new Millisecond(), y);
}
/**
* Adds an observation to the 'free memory' time series.
*
* @param y the free memory.
*/
private void addFreeObservation(double y) {
free.add(new Millisecond(), y);
}
/**
* The data generator.
*/
class DataGenerator extends Timer implements ActionListener {
/**
* Constructor.
*/
DataGenerator() {
super(100, null);
addActionListener(this);
}
/**
* Adds a new free/total memory reading to the dataset.
*
* @param event the action event.
*/
public void actionPerformed(ActionEvent event) {
long f = Runtime.getRuntime().freeMemory();
long t = Runtime.getRuntime().totalMemory();
addTotalObservation(t);
addFreeObservation(f);
}
}
/**
* Entry point for the sample application.
*
* @param args ignored.
*/
public static void main(String[] args) {
JFrame frame = new JFrame("Memory Usage Demo");
MemoryUsage panel = new MemoryUsage();
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setBounds(200, 120, 600, 280);
frame.setVisible(true);
panel.new DataGenerator().start();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}