我是第一次使用JFreeChart,并且正在使用TimeSeriesCollection()创建一个TimeSeriesChart。
我从数据库查询中得到的reslutset是app。已有1000条记录。我正在使用 org.jfree.date.time.Minute.Minute(int min …) 对象将其添加到TimeSeries对象。
我有一个JFrame,可直接在其上添加ChartPanel。用户将提供新的输入参数,并使用新的数据集重新加载图表数据。因此,我会在每次 重新加载 前通过以下方法进行清理:
dataset.removeAllSeries(); chart.removeLegend(); chart.getRenderingHints().clear(); cp.getChartRenderingInfo().setEntityCollection(null); cp.removeAll(); cp.revalidate();
输出是完美的。但是我注意到,在“多次在Eclipse中”运行该程序后,我看到以下有关Java堆空间的错误消息。有时,即使数据集很小(100条记录),我也可以在任务管理器中看到程序在PC内存上运行。
Exception occurred during event dispatching: java.lang.OutOfMemoryError: Java heap space at sun.util.calendar.Gregorian.newCalendarDate(Gregorian.java:67) at java.util.GregorianCalendar.<init>(GregorianCalendar.java:575) at java.util.Calendar.createCalendar(Calendar.java:1012) at java.util.Calendar.getInstance(Calendar.java:964) at org.jfree.chart.axis.DateTickUnit.addToDate(DateTickUnit.java:238) at org.jfree.chart.axis.DateAxis.refreshTicksHorizontal(DateAxis.java:1685) at org.jfree.chart.axis.DateAxis.refreshTicks(DateAxis.java:1556) at org.jfree.chart.axis.ValueAxis.reserveSpace(ValueAxis.java:809) at org.jfree.chart.plot.XYPlot.calculateDomainAxisSpace(XYPlot.java:3119) at org.jfree.chart.plot.XYPlot.calculateAxisSpace(XYPlot.java:3077) at org.jfree.chart.plot.XYPlot.draw(XYPlot.java:3220) at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1237) at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1677) at javax.swing.JComponent.paint(JComponent.java:1029) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5124) at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1491) at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1422) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:294) at javax.swing.RepaintManager.paint(RepaintManager.java:1225) at javax.swing.JComponent._paintImmediately(JComponent.java:5072) at javax.swing.JComponent.paintImmediately(JComponent.java:4882) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:786) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714) at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:694) at javax.swing.RepaintManager.access$700(RepaintManager.java:41) at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1636) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:646) at java.awt.EventQueue.access$000(EventQueue.java:84) at java.awt.EventQueue$1.run(EventQueue.java:607) at java.awt.EventQueue$1.run(EventQueue.java:605) at java.security.AccessController.doPrivileged(Native Method)
我的申请如下:
我有一个JFrame,在将图表传递给它之后,可以直接在其上添加ChartPanel。
chart = ChartFactory.createTimeSeriesChart("Peak monitor", , "Time: Zoom in", "# of Requests Logged", createDataset(from,to), true, false, false); chartpanel = new ChartPanel(chart); FramePanel.this.add(cp); validate();
这里 createDataset(from,to) 是一个方法
private TimeSeriesCollection createDataset(Date from, Date to) { dataset.addSeries(controller.getStuff(from, to)); return dataset; }
__在SwingWorker线程中调用 getStuff (DIBkgd方法)
public TimeSeries getStuff(Date from, Date to) { s1 = new TimeSeries("Log Requests"); final Date from1 = from; final Date to1 = to; progressDialog.setVisible(true); sw = new SwingWorker<Void, Integer>() { @Override protected Void doInBackground() throws Exception { if (db.getCon() == null) { db.connect(); } Arrlst2.clear(); Arrlst2= db.getDataDB(from1, to1); for (Qryobjects x : Arrlst2) { s1.add(new Minute(x.getMinute(), x.getHour(), x.getDay(), x.getMonth(), x.getYear()), x.getCount()); } System.out.println("finished fetching data"); return null; } @Override protected void done() { progressDialog.setVisible(false); } }; sw.execute(); return s1; }
在我的数据库类中,执行 getDataDB :
public List<Qryobjects> getDataDB(Date from, Date to) { PreparedStatement select; ResultSet rs; String selectSql = "Select Sum(Cnt) Cid, Hr, Min, Dat from (Select count(H.Request_Id) Cnt , To_Char(H.Timestamp,'HH24') HR, To_Char(H.Timestamp,'mm') MIN, To_Char(H.Timestamp,'MM-dd-yyyy') DAT From Status_History H Where H.Timestamp Between ? And ? Group By H.Request_Id, H.Timestamp Order By H.Timestamp Asc) Group By Hr, Min, Dat order by Dat asc"; try { select = con.prepareStatement(selectSql); select.setDate(1, from); select.setDate(2, to); rs = select.executeQuery(); System.setProperty("true", "true"); while (rs.next()) { int cnt = rs.getInt("cid"); int hour = Integer.parseInt(rs.getString("Hr")); int min = Integer.parseInt(rs.getString("Min")); int month = Integer.parseInt(rs.getString("dat").substring(0, 2)); int day = Integer.parseInt(rs.getString("dat").substring(3, 5)); int year = Integer.parseInt(rs.getString("dat").substring(6, 10)); Arrlst1.add(new Qryobjects(cnt, hour, min, day, month,year)); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } return Arrlst1; }
我解决了我的问题。
我从@TrashGod那里得到了使用dispose()的线索。但这对我不直接起作用。
我将图表面板直接添加到我的主JFrame容器中。就我而言,我想一遍又一遍地在同一JFrame容器中创建图表。
我首先尝试清除数据集,然后在图表面板上调用removeall(),但没有帮助。
然后,我找到的解决方案是创建另一个JFrame并将图表面板添加到其中。当我关闭此JFrame时,我再次清除数据集,并在图表面板上调用removeall(),也称为dispose()。因此,每次创建新图表时,都会创建此JFrame及其子组件,并在退出该JFrame时将其完全废弃。
因此,当创建图表时,将创建一个新的JFrame,然后对其进行处理。
我还应该补充一点,进行此更改后,我开始在Java VisualVM Profiler中看到“锯齿”模式。我还使用了Jprofiler,当我在运行程序时看到超过100,000个对象被创建时,我感到震惊。现在,我看到创建了9000个对象,并且对于JFree包它保持不变,并且根据检索到的结果集,数据库包中对象的数量增加或减少。
我要做的另一件事是使我的SQL进行解析并将其转换为数字。我想减少创建的对象数量,还希望减少程序对每个检索到的记录所做的处理。