Now the app does two things:
You can add and remove datasets by means of a JCheckBox. Here I remark that removing a dataset (setting it to null) has absolutely no effect on memory use. No memory is freed. This is not that bad because the null datasets are reused and the amount of memory doesn't get higher than the maximum number of datasets ever needed.
The application also has a refreh Button to view data for a different time period. And here I have a real problem. The application needs about 45m at rest. When I add 3 relatively simple series (3000 items) to the plot, it still needs about the same amount of memory. When I refresh the plot by removing datasets and adding them again, memory use raises to 75m, the second time to 95m and then stays at 95 where it stays, even if I refresh more often. If I add more or bigger datasets to the plot, it runs into OutOfMemory at the first refresh.
Now can anyone explain this behaviour or parts of it? Disposing the JFrame containing the application is not releasing resources either. Is there anything to be done? I included the refreshing code, which is not very complicated (notice that removeDataset() is, as described above, a custom method that sets the dataset to null and addDataset() is a wrapper for setDataset that reuses null Datasets).
Code: Select all
private void repaintPlot(String begin, String end, String intervalString) {
// this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
// try { //OutOfMemoryError
int interval = Integer.valueOf(intervalString);
if (checkDates(begin, end, intervalString)) {
for (int i = 1; i < plot.getDatasetCount(); i++) {
if (plot.getDataset(i) != null) {
plot.removeDataset(i, true, (String)plot.getDataset(i).getSeriesKey(0)+"\n");
}
}
System.gc();
GregorianCalendar greg;
XYSeriesCollection dataset;
XYSeries series;
Object[] currentData;
for (int i = 0; i < tableModel.getRowCount(); i++ ) {
if (tableModel.getValueAt(i, 1).equals(true)) {
dataset = new XYSeriesCollection();
String seriesLabel = (String)(tableModel.getValueAt(i, 3))+" in "+(String)(tableModel.getValueAt(i, 5));
series = new XYSeries(seriesLabel);
greg = DateUtil.parseDateTime("-", ":", begin, DateUtil.YYMMDD);
currentData = dataModel.getData(DateUtil.parseDateTime(DateUtil.YYMMDD, "-", ":", begin),
DateUtil.parseDateTime(DateUtil.YYMMDD, "-", ":", end), interval, i, false);
int anzahlDatensaetze = ((long[]) currentData[0]).length;
for (int j = 0; j < anzahlDatensaetze; j++) {
series.add(((long[])currentData[0])[j], ((double[])currentData[1])[j], false);
}
dataset.addSeries(series);
XYItemRenderer rend = getRenderer((String)tableModel.getValueAt(i, 0));
plot.addDataset(dataset, rend, 1.0, 0, seriesLabel+"\n");
hashtable.put((String)(tableModel.getValueAt(i, 2)), plot.getIndexOfDataset(dataset));
indexArray[i] = true;
}
}
}
/*
} catch (java.lang.OutOfMemoryError error) {
JOptionPane.showMessageDialog(this, "The Java Virtual Machine is out of memory.\n Try " +
"to use a smaller time period or a bigger interval or restart the application. You" +
"can also try to allocate more memory.\n" +
"Class Auswertung in Package dscaem.", null, JOptionPane.ERROR_MESSAGE);
System.out.println(error.toString());
} finally {
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
//hier sollten die Häkchen wieder rausgenommen werden, aber das würde den Listener
//aktivieren und removeDataset() würde einen Fehler verursachen
}*/
}