Memory Panel

This component will show the memory usage in a chart. The chart will roll from right to left and show a constant set of sample points. The dataset will be a set of XY-series, whereby we currently only need one XY-series. This dataset will be shown as a XY-area. Since the dataset will be sampled in seconds we will need a number X-axis. Similarly for the memory usage we will need a number Y-axis. The constructor of the MemoryPanel class will do the correspond-ing setup.

    /**
     * <p>Create a free chart panel.</p>
     * <p>The layout is basically:</p>
     * <pre>
 * +--+-----------------------------------+ * |N | | * |uA| | * |mx| | * |bi| XYArea | * |es| | * |r | | * +--+-----------------------------------+ * | | NumberAxis | * +--+-----------------------------------+ * <pre> * * @param n The numnber of time series. */ MemoryPanel(int n) {

More details on the internals of the constructor can be found in the appendix where a full listing of the Java code is given. We basically assemble a chart from the JFreeChart libraries. The chart will be stored in a field of the memory panel, so that it can be later accessed by the addValues() method. This method is responsible for filling the dataset. The method first appends a new data to the right:

        FixedMillisecond whenobj = new FixedMillisecond(when);
        for (int i = 0; i < dataset.getSeriesCount(); i++) {
            XYSeries series = dataset.getSeries(i);
            series.add(whenobj, values[i]);
        }

Without further measures the dataset would accumulate more and more data. We limit the number of Y values by the constant VALUES_LIMIT. We then use a counter to check whether we have reached the limit. If the limit has reached we remove Y values and corresponding X values from the left. The VALUES_LIMIT is currently fixed to 200. Higher values mean more time spent by the chart adjusting and painting the chart. Which values guarantee a smooth scrolling of the chart depends on the machine performance and on the update frequency of the frame thread.

        count++;
        while (count >= VALUES_LIMIT) {
            for (int i = 0; i < dataset.getSeriesCount(); i++) {
                XYSeries series = dataset.getSeries(i);
                series.delete(0, 0);
            }
            count--;
        }

We had first experimented with the NetBeans/VisualVM chart. This chart was only able automatically increase the Y axis scale, but not to the decrease the Y axis scale. But this can happen since we use a sliding window. We therefore switched to JFreeChart.

Kommentare