10.3 实例应用
10.3.1 概述
实例MemoryUsageDemo.java文档可以从下面的链接中获得:
http://www.object-refinery.com/jfreechart/premium/index.html
页面需要输入购买JFreeChart开发指南时需要的用户名和密码。
10.3.2 创建一个dataset
创建的实例代码数据源,包含了一个单一的时序集合,集合内有两个TimeSeries对象(一个是计算总内存,另一个是计算剩余内存)。代码如下:
this.total = new TimeSeries("Total", Millisecond.class);
this.total.setMaximumItemAge(30000);
this.free = new TimeSeries("Free", Millisecond.class);
this.free.setMaximumItemAge(30000);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(this.total);
dataset.addSeries(this.free);
每个时间系列的maximumItemAge属性设置为30000毫秒(30秒)。因此任何时候添加到新系列的新数据,都是30秒前记录的老数据。
10.3.3 创建一个图表
图表的创建(定制)都遵循所有图表创建的标准模式。创建动态图也是一样,没有任何特殊的步骤。除了我们将autoRange属性设置为true之外。同时,这也有利于维护图表使用的数据源的引用。
10.3.4 更新一个dataset
在本实例演示中,通过向两个时序图添加的数据来更新数据源,该数据的添加有一个独立的线程Timer管理。代码如下:
class DataGenerator extends Timer implements ActionListener {
DataGenerator(int i) {
super(i, null);
addActionListener(this);
}
public void actionPerformed(ActionEvent actionevent) {
long l = Runtime.getRuntime().freeMemory();
long l_0_ = Runtime.getRuntime().totalMemory();
MemoryUsageDemo.this.addTotalObservation((double) l_0_);
MemoryUsageDemo.this.addFreeObservation((double) l);
}
}
注意JFreeChart在画图表和数据源更新代码之间没有使用线程同步,因此是不安全的。另一点需要注意的是,曾做过一个关于JFreeChart内存泄露问题的测试,将JFreeChart的实例在一个机器上连续运行6天。随着图表的不断更新,我们就可以看到垃圾收集器所产生的影响。六天之后,发现总内存的使用量保持不变。当JFreChart产生并丢弃的临时对象(垃圾对象)时,剩余可用内存减少了。增加的使用内存量是垃圾收集器工作时所使用的。
10.3.5 全部代码
下面是全部的实例代码:
/* MemoryUsageDemo - Decompiled by JODE
* Visit http://jode.sourceforge.net/
*/
package demo;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
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.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;
public class MemoryUsageDemo extends JPanel {
private static final long serialVersionUID = 6776712838359498649L;
private TimeSeries total;
private TimeSeries free;
/* synthetic */
static Class class$org$jfree$data$time$Millisecond;
class DataGenerator extends Timer implements ActionListener {
private static final long serialVersionUID = 1L;
DataGenerator(int i) {
super(i, null);
addActionListener(this);
}
public void actionPerformed(ActionEvent actionevent) {
long l = Runtime.getRuntime().freeMemory();
long l_0_ = Runtime.getRuntime().totalMemory();
MemoryUsageDemo.this.addTotalObservation((double) l_0_);
MemoryUsageDemo.this.addFreeObservation((double) l);
}
}
public MemoryUsageDemo(int i) {
super(new BorderLayout());
total = new TimeSeries(
"Total Memory",
(class$org$jfree$data$time$Millisecond == null ? (class$org$jfree$data$time$Millisecond = class$("org.jfree.data.time.Millisecond"))
: class$org$jfree$data$time$Millisecond));
total.setMaximumItemAge((long) i);
free = new TimeSeries(
"Free Memory",
(class$org$jfree$data$time$Millisecond == null ? (class$org$jfree$data$time$Millisecond = class$("org.jfree.data.time.Millisecond"))
: class$org$jfree$data$time$Millisecond));
free.setMaximumItemAge((long) i);
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
timeseriescollection.addSeries(total);
timeseriescollection.addSeries(free);
DateAxis dateaxis = new DateAxis("Time");
NumberAxis numberaxis = new NumberAxis("Memory");
dateaxis.setTickLabelFont(new Font("SansSerif", 0, 12));
numberaxis.setTickLabelFont(new Font("SansSerif", 0, 12));
dateaxis.setLabelFont(new Font("SansSerif", 0, 14));
numberaxis.setLabelFont(new Font("SansSerif", 0, 14));
XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer(
true, false);
xylineandshaperenderer.setSeriesPaint(0, Color.red);
xylineandshaperenderer.setSeriesPaint(1, Color.green);
xylineandshaperenderer.setSeriesStroke(0, new BasicStroke(3.0F, 0, 2));
xylineandshaperenderer.setSeriesStroke(1, new BasicStroke(3.0F, 0, 2));
XYPlot xyplot = new XYPlot(timeseriescollection, dateaxis, numberaxis,
xylineandshaperenderer);
xyplot.setBackgroundPaint(Color.lightGray);
xyplot.setDomainGridlinePaint(Color.white);
xyplot.setRangeGridlinePaint(Color.white);
xyplot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
dateaxis.setAutoRange(true);
dateaxis.setLowerMargin(0.0);
dateaxis.setUpperMargin(0.0);
dateaxis.setTickLabelsVisible(true);
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
JFreeChart jfreechart = new JFreeChart("JVM Memory Usage", new Font(
"SansSerif", 1, 24), xyplot, true);
jfreechart.setBackgroundPaint(Color.white);
ChartPanel chartpanel = new ChartPanel(jfreechart, true);
chartpanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createEmptyBorder(4, 4, 4, 4), BorderFactory
.createLineBorder(Color.black)));
add(chartpanel);
}
private void addTotalObservation(double d) {
total.add(new Millisecond(), d);
}
private void addFreeObservation(double d) {
free.add(new Millisecond(), d);
}
public static void main(String[] strings) {
JFrame jframe = new JFrame("Memory Usage Demo");
MemoryUsageDemo memoryusagedemo = new MemoryUsageDemo(30000);
jframe.getContentPane().add(memoryusagedemo, "Center");
jframe.setBounds(200, 120, 600, 280);
jframe.setVisible(true);
memoryusagedemo.new DataGenerator(100).start();
jframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowevent) {
System.exit(0);
}
});
}
/* synthetic */static Class class$(String string) {
Class var_class;
try {
var_class = Class.forName(string);
} catch (ClassNotFoundException classnotfoundexception) {
throw new NoClassDefFoundError(classnotfoundexception.getMessage());
}
return var_class;
}
}