001/* --------------------
002* MemoryUsageDemo.java
003* --------------------
004* (C) Copyright 2002-2006, by Object Refinery Limited.
005*/
006package ca.bc.webarts.tools;
007
008import java.awt.BasicStroke;
009import java.awt.BorderLayout;
010import java.awt.Color;
011import java.awt.Font;
012import java.awt.event.ActionEvent;
013import java.awt.event.ActionListener;
014import java.awt.event.WindowAdapter;
015import java.awt.event.WindowEvent;
016import javax.swing.BorderFactory;
017import javax.swing.JFrame;
018import javax.swing.JPanel;
019import javax.swing.Timer;
020import org.jfree.chart.ChartPanel;
021import org.jfree.chart.JFreeChart;
022import org.jfree.chart.axis.DateAxis;
023import org.jfree.chart.axis.NumberAxis;
024import org.jfree.chart.plot.XYPlot;
025import org.jfree.chart.renderer.xy.XYItemRenderer;
026import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
027import org.jfree.data.time.Millisecond;
028import org.jfree.data.time.TimeSeries;
029import org.jfree.data.time.TimeSeriesCollection;
030import org.jfree.ui.RectangleInsets;
031
032/**
033* A demo application showing a dynamically updated chart that displays the
034* current JVM memory usage.
035* <p>
036* IMPORTANT NOTE: THIS DEMO IS DOCUMENTED IN THE JFREECHART DEVELOPER GUIDE.
037* DO NOT MAKE CHANGES WITHOUT UPDATING THE GUIDE ALSO!!
038*/
039public class MemoryUsageDemo extends JPanel {
040/** Time series for total memory used. */
041private TimeSeries total;
042/** Time series for free memory. */
043private TimeSeries free;
044
045/**
046* Creates a new application.
047*
048* @param maxAge the maximum age (in milliseconds).
049*/
050public MemoryUsageDemo(int maxAge) {
051super(new BorderLayout());
052// create two series that automatically discard data more than 30
053// seconds old...
054this.total = new TimeSeries("Total Memory", Millisecond.class);
055this.total.setMaximumItemAge(maxAge);
056this.free = new TimeSeries("Free Memory", Millisecond.class);
057this.free.setMaximumItemAge(maxAge);
058TimeSeriesCollection dataset = new TimeSeriesCollection();
059dataset.addSeries(this.total);
060dataset.addSeries(this.free);
061DateAxis domain = new DateAxis("Time");
062NumberAxis range = new NumberAxis("Memory");
063domain.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 12));
064range.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 12));
065domain.setLabelFont(new Font("SansSerif", Font.PLAIN, 14));
066range.setLabelFont(new Font("SansSerif", Font.PLAIN, 14));
067XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
068renderer.setSeriesPaint(0, Color.red);
069renderer.setSeriesPaint(1, Color.green);
070renderer.setStroke(new BasicStroke(3f, BasicStroke.CAP_BUTT,
071BasicStroke.JOIN_BEVEL));
072XYPlot plot = new XYPlot(dataset, domain, range, renderer);
073plot.setBackgroundPaint(Color.lightGray);
074plot.setDomainGridlinePaint(Color.white);
075plot.setRangeGridlinePaint(Color.white);
076plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
077domain.setAutoRange(true);
078domain.setLowerMargin(0.0);
079domain.setUpperMargin(0.0);
080domain.setTickLabelsVisible(true);
081range.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
082JFreeChart chart = new JFreeChart("JVM Memory Usage",
083new Font("SansSerif", Font.BOLD, 24), plot, true);
084chart.setBackgroundPaint(Color.white);
085ChartPanel chartPanel = new ChartPanel(chart);
086chartPanel.setBorder(BorderFactory.createCompoundBorder(
087BorderFactory.createEmptyBorder(4, 4, 4, 4),
088BorderFactory.createLineBorder(Color.black)));
089add(chartPanel);
090}
091
092
093/**
094* Adds an observation to the ’total memory’ time series.
095*
096* @param y the total memory used.
097*/
098private void addTotalObservation(double y) {
099this.total.add(new Millisecond(), y);
100}
101
102
103/**
104* Adds an observation to the ’free memory’ time series.
105*
106* @param y the free memory.
107*/
108private void addFreeObservation(double y) {
109this.free.add(new Millisecond(), y);
110}
111
112
113/**
114* The data generator.
115*/
116class DataGenerator extends Timer implements ActionListener {
117/**
118* Constructor.
119*
120* @param interval the interval (in milliseconds)
121*/
122DataGenerator(int interval) {
123super(interval, null);
124addActionListener(this);
125}
126
127
128/**
129* Adds a new free/total memory reading to the dataset.
130*
131* @param event the action event.
132*/
133public void actionPerformed(ActionEvent event) {
134  long f = Runtime.getRuntime().freeMemory();
135  long t = Runtime.getRuntime().totalMemory();
136  addTotalObservation(t);
137  addFreeObservation(f);
138}
139}
140
141
142/**
143* Entry point for the sample application.
144*
145* @param args ignored.
146*/
147public static void main(String[] args)
148{
149  JFrame frame = new JFrame("Memory Usage Demo");
150  MemoryUsageDemo panel = new MemoryUsageDemo(30000);
151  frame.getContentPane().add(panel, BorderLayout.CENTER);
152  frame.setBounds(200, 120, 600, 280);
153  frame.setVisible(true);
154  panel.new DataGenerator(100).start();
155  frame.addWindowListener(new WindowAdapter()
156  {
157    public void windowClosing(WindowEvent e)
158    {
159      System.exit(0);
160    }
161  });
162}
163}