Google Chart Constant Redrawing Memory Increase

后端 未结 2 1957
北恋
北恋 2021-02-10 05:11

I\'m redrawing the chart based on new data each second, and it works and looks great, but I notice it increase memory like 1 MB usage each second. Any way to fix this? I notice

2条回答
  •  抹茶落季
    2021-02-10 05:18

    I've solved this by globaly store the chart. Before draw the chart, you need to check if chart has been instantiated. If not, create the new chart object else call clearChart() method before draw it. Like this:

    //Store all chart objects in a global array to avoid memory leak
    var charts = [];
    
    function drawChart(chartName, element, data, chartOptions) {
       if (charts[chartName] === undefined || charts[chartName] === null) {
              charts[chartName] = new google.visualization.LineChart(group);
       } else {
              charts[chartName].clearChart();
       }
       charts[chartName].draw(dataTable, chartOptions);
    }
    

提交回复
热议问题