问题
I have 10 charts in my page and I refresh them using poll
component every 1 minute.
But after one hour i see that firefox and chrome got more that 1 GB of memory and sometimes even firefox crashes.
Is this a memory leak caused by primefaces chart? How can I solve the problem?
Here is the source code for my example application:
In this code interval is set to 3 seconds to see the problem sooner!
<h:body>
<h:form id="timerForm">
<p:poll interval="3" widgetVar="timer" update=":chartPanel" autoStart="true" />
</h:form>
<p:panelGrid columns="2" id="chartPanel">
<p:lineChart id="chart1" value="#{chartController.model}"
legendPosition="nw" style="height:200px;width: 500px;" minY="0" />
<p:lineChart id="chart2" value="#{chartController.model}"
legendPosition="nw" style="height:200px;width: 500px;" minY="0" />
<p:lineChart id="chart3" value="#{chartController.model}"
legendPosition="nw" style="height:200px;width: 500px;" minY="0" />
<p:lineChart id="chart4" value="#{chartController.model}"
legendPosition="nw" style="height:200px;width: 500px;" minY="0" />
<p:lineChart id="chart5" value="#{chartController.model}"
legendPosition="nw" style="height:200px;width: 500px;" minY="0" />
<p:lineChart id="chart6" value="#{chartController.model}"
legendPosition="nw" style="height:200px;width: 500px;" minY="0" />
</p:panelGrid>
</h:body>
And here is the bean:
@Named
@RequestScoped
public class ChartController {
static final Logger log = Logger.getLogger(ChartController.class.getName());
@PostConstruct
private void init() {
}
private ChartSeries getData(String label) {
ChartSeries data = new ChartSeries();
data.setLabel(label);
for (int i = 1; i <= 20; i++) {
data.set(i, Math.random() * 1000);
}
if (data.getData().isEmpty()) {
data.set(0, 0);
}
log.log(Level.INFO, "Chart loaded for :{0}", label);
return data;
}
public CartesianChartModel getModel() {
CartesianChartModel chartModel = new CartesianChartModel();
chartModel.addSeries(getData("Data 1"));
chartModel.addSeries(getData("Data 2"));
chartModel.addSeries(getData("Data 3"));
return chartModel;
}
}
When I close the browser, it completely releases memory. And here is the source code to download: I have uploaded the source code, it's a maven project, just download it and open it in your IDE and deploy it in your app server.
https://dl.dropbox.com/s/secmuo7vjasdaue/chart-bug.zip?dl=1
回答1:
The component does not destroy resources before refreshing. I found that this is a bug in version 3.3.1 and is fixed in 3.4-SNAPSHOT
Here is the reference:
http://forum.primefaces.org/viewtopic.php?f=3&t=18286&hilit=chart+memory+leak
http://code.google.com/p/primefaces/issues/detail?id=4183
来源:https://stackoverflow.com/questions/11643630/primefaces-chart-memory-leak-when-refreshing-with-poll-component