I'm trying to create a chart of thumbnails of some data with using JScrollPane
, but I encounter performance difficulties. This example has about 100 thumbnails charts with 5000 samples in each one. When I'm trying to scroll down and back to up multiple times, scrolling occurs with delay, CPU load increasing, application memory usage reaches over 500 Mb.
Is there a way to avoid this performance problem without reducing my data?
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.ThermometerPlot;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class ThumbnailChartsTest extends JPanel {
private static final int W = 200;
private static final int H = W;
private static final int N = 5000;
private static final Random random = new Random();
private static ChartPanel createPane() {
final XYSeries series = new XYSeries("Data");
for (int i = 0; i < random.nextInt(N) + N; i++) {
series.add(i, random.nextGaussian());
}
XYSeriesCollection dataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYLineChart("Random", "Domain",
"Range", dataset, PlotOrientation.VERTICAL, false, false, false);
return new ChartPanel(chart, W, H, W, H, W, H,
false, true, true, true, true, true);
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 4));
for (int i=0; i<100; i++){
panel.add(createPane());
}
JScrollPane scrollPane = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
f.add(scrollPane);
f.pack();
f.setVisible(true);
}
});
}
}
Edit: I can't understand one thing: Why in this case memory usage still very huge! Please look at this illustration.
Addition: I think there is some misunderstanding.
Heap size by monitor visualVM
After starting applet heap size is only 125 Mb, it's cool.
But then I'm starting testing: scrolling and resizing multiple times, more and more -- up and down, up and down, smaller frame and bigger frame. Heap size growing up over 500 Mb! I suppose this situation isn't normal.
Addition #2
Real-world example:
My data has size about only 2 Mb and represented in 90 charts(2 series in each one), one series contain 3000 elements. I've implemented changing number columns by slider.
But for this small data heap size growing up over 1.5 GB!
This happens after some actions, changing number columns e.g. For my CPU(core 2 duo 2.2GHz) every drawing table takes time about 4 sec! With this big delay it's hard to control slider.
update:
I've implemented downsampling my data to 100 samples per thumbnail chart.
Now It's definitely faster, but problem with HUGE heap size still there. On the picture the one is above 700Mb, and it's not a record. I'm frustrated.