我想像JConsole那样监视堆内存。我正在运行以下程序,每一秒后我都希望看到堆增长,因为我在每个回合中创建20个线程,但它没有在图中增长。请告诉我如何通过java程序检查堆内存。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.*;
public class MemoryUsageProgram extends JPanel
{
class DataGenerator extends Timer
implements ActionListener
{
public void actionPerformed(ActionEvent actionevent)
{
for(int i=1;i<20;i++) {
IoBoundJob job=new IoBoundJob();
job.start();
}
System.gc();
long l = Runtime.getRuntime().freeMemory();
long l1 = Runtime.getRuntime().totalMemory();
long usedMB = (l1 - l)/1024/1024 ;
plotUsedMemory(usedMB);
}
DataGenerator(int i)
{
super(i, null);
addActionListener(this);
}
}
private TimeSeries used;
public MemoryUsageProgram()
{
super(new BorderLayout());
used = new TimeSeries("used");
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
timeseriescollection.addSeries(used);
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(3F, 0, 2));
xylineandshaperenderer.setSeriesStroke(1, new BasicStroke(3F, 0, 2));
XYPlot xyplot = new XYPlot(timeseriescollection, dateaxis, numberaxis, xylineandshaperenderer);
dateaxis.setAutoRange(true);
dateaxis.setLowerMargin(0.0D);
dateaxis.setUpperMargin(0.0D);
dateaxis.setTickLabelsVisible(true);
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
JFreeChart jfreechart = new JFreeChart("Memory Usage", new Font("SansSerif", 1, 24), xyplot, true);
ChartUtilities.applyCurrentTheme(jfreechart);
ChartPanel chartpanel = new ChartPanel(jfreechart, true);
chartpanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4), BorderFactory.createLineBorder(Color.black)));
add(chartpanel);
}
private void plotUsedMemory(double d)
{
used.add(new Millisecond(), d);
}
public static void main(String args[])
{
JFrame jframe = new JFrame("Memory Usage");
MemoryUsageProgram memoryusagedemo = new MemoryUsageProgram();
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);
}
});
}
}
import java.util.concurrent.TimeUnit;
public class IoBoundJob extends Thread {
public IoBoundJob() {
}
@Override
public void run() {
try{
Thread.currentThread().sleep(250);
} catch(InterruptedException e) {
System.out.println("from IOBoundJob Class");
}
}
}