1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.rt.swingmonitor;
22
23
24
25 import java.awt.*;
26 import java.awt.event.*;
27 import javax.swing.*;
28 import org.jfree.chart.*;
29 import org.jfree.chart.axis.DateAxis;
30 import org.jfree.chart.axis.NumberAxis;
31 import org.jfree.chart.plot.XYPlot;
32 import org.jfree.chart.renderer.xy.DefaultXYItemRenderer;
33 import org.jfree.data.time.*;
34 import org.jfree.ui.Spacer;
35
36 /***
37 * Show Memory usage in Monitor Application.
38 * This is different from client (Application under test) Memory usage.
39 *
40 * Created on 15.12.2004
41 * Contributing Author(s):
42 *
43 * Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
44 * Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
45 *
46 * @author vlads
47 * @version $Revision: 1.3 $ ($Author: vlads $) $Date: 2004/12/16 05:35:51 $
48 */
49 public class MonitorMemoryUsagePanel extends MonitoringJPanel {
50
51 private TimeSeries total;
52
53 private TimeSeries free;
54
55 class MemoryDataGenerator extends Timer implements ActionListener {
56
57 public MemoryDataGenerator(int delay) {
58 super(delay, null);
59 addActionListener(this);
60 }
61
62 public void actionPerformed(ActionEvent actionevent) {
63 updateData();
64 }
65 }
66
67 public MonitorMemoryUsagePanel(int historyMin, int delay) {
68 setLayout(new BorderLayout());
69 int periods = historyMin * 60 * 1000;
70
71 total = new TimeSeries("Total Memory", org.jfree.data.time.Millisecond.class);
72 total.setHistoryCount(periods);
73 free = new TimeSeries("Free Memory", org.jfree.data.time.Millisecond.class);
74 free.setHistoryCount(periods);
75 TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
76 timeseriescollection.addSeries(total);
77 timeseriescollection.addSeries(free);
78 DateAxis dateaxis = new DateAxis("Time");
79 NumberAxis numberaxis = new NumberAxis("Memory (MB)");
80 dateaxis.setTickLabelFont(new Font("SansSerif", 0, 12));
81 numberaxis.setTickLabelFont(new Font("SansSerif", 0, 12));
82 dateaxis.setLabelFont(new Font("SansSerif", 0, 14));
83 numberaxis.setLabelFont(new Font("SansSerif", 0, 14));
84 DefaultXYItemRenderer defaultxyitemrenderer = new DefaultXYItemRenderer();
85 defaultxyitemrenderer.setSeriesPaint(0, Color.red);
86 defaultxyitemrenderer.setSeriesPaint(1, Color.green);
87 defaultxyitemrenderer.setStroke(new BasicStroke(3F, 0, 2));
88 XYPlot xyplot = new XYPlot(timeseriescollection, dateaxis, numberaxis, defaultxyitemrenderer);
89 xyplot.setBackgroundPaint(Color.lightGray);
90 xyplot.setDomainGridlinePaint(Color.white);
91 xyplot.setRangeGridlinePaint(Color.white);
92 xyplot.setAxisOffset(new Spacer(1, 5D, 5D, 5D, 5D));
93 dateaxis.setAutoRange(true);
94 dateaxis.setLowerMargin(0.0D);
95 dateaxis.setUpperMargin(0.0D);
96 dateaxis.setTickLabelsVisible(true);
97 numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
98
99 JFreeChart jfreechart = new JFreeChart("Analyzer JVM Memory Usage", new Font("SansSerif", 1, 24), xyplot, true);
100 jfreechart.setBackgroundPaint(Color.white);
101
102 ChartPanel chartpanel = new ChartPanel(jfreechart);
103 chartpanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4),
104 BorderFactory.createLineBorder(Color.black)));
105
106 StandardLegend standardlegend = (StandardLegend) jfreechart.getLegend();
107 standardlegend.setItemFont(new Font("SansSerif", 0, 12));
108
109 chartpanel.setMouseZoomable(true, false);
110
111 add(chartpanel);
112
113 updateData();
114
115 (new MemoryDataGenerator(delay)).start();
116 }
117
118 public boolean canCloseTab() {
119 return false;
120 }
121
122
123 public void updateData() {
124 long freeMemory = Runtime.getRuntime().freeMemory();
125 long totalMemory = Runtime.getRuntime().totalMemory();
126 addTotalObservation(totalMemory / (1024 * 1024));
127 addFreeObservation(freeMemory / (1024 * 1024));
128 }
129
130 private void addTotalObservation(double d) {
131 total.add(new Millisecond(), d);
132 }
133
134 private void addFreeObservation(double d) {
135 free.add(new Millisecond(), d);
136 }
137
138 }