Memory leak on rebuilding charts...

Hi there,

We have a massive problem with Syncfusion charts for flutter. Whenever the chart gets rebuilt (i.e. setState is called), the chart is leaking memory.

We have created a simple reproduction, that shows the problem. Every time the chart is rebuilt (which in our case can be a lot depending on user actions), the chart points seem to be doubled. here is a snippet (with full source code attached)

import 'dart:async';
import 'dart:math';

import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:flutter/material.dart';

final Random random = Random();

class Chart extends StatefulWidget {
// ignore: prefer_const_constructors_in_immutables
Chart({Key? key}) : super(key: key);

@override
_ChartState createState() => _ChartState();
}

class _ChartState extends State<Chart> {
@override
void initState() {
super.initState();

Timer.periodic(
const Duration(milliseconds: 100),
(Timer t) => setState(() {}),
);
}

final zoomPan = ZoomPanBehavior();

final myAxis = NumericAxis();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Memory Leak Demo')),
body: SfCartesianChart(
primaryXAxis: myAxis,
series: [
LineSeries<_SalesData, double>(
dataSource: List.generate(
1000,
(index) => _SalesData(index.toDouble(), index.toDouble()),
),
xValueMapper: (_SalesData sales, _) => sales.year,
yValueMapper: (_SalesData sales, _) => sales.sales,
),
],
zoomPanBehavior: zoomPan,
),
);
}
}

class _SalesData {
_SalesData(this.year, this.sales);

final double year;
final double sales;
}


A screenshot for the memory usage (after a minute or two) can be seen here:
 two)
MemoryUsage.png

Thanks in advance for the help!


Kind regards,

Luca from Gutta



Attachment: syncfusion_memory_leak_4903e7ea.zip


7 Replies

YG Yuvaraj Gajaraj Syncfusion Team March 20, 2022 04:48 PM UTC

Hi Luca, 
 
We ensured that the memory leak happen due to calling setState over every 100 milliseconds, we regret to use the setState for updating the data source because while calling the setState it rebuilds the whole chart widget instead of updating the data source alone. So, here we suggest the updateDataSource public method to update the data source in the chart. It will update the latest data point added in the list and does not rebuild the whole chart widget. We have modified the provided sample and attached it below for your reference. 
 
         
 
  
 
Regards, 
Yuvaraj. 



LU Luca March 20, 2022 09:01 PM UTC

Dear Yuvaraj, this is not a fix, but simply a workaround. We also need to update the chart widget whenever the axes change for example, or whenever something else such as chart annotations changes. 

What is causing the memory leak when rebuilding the chart widget? This should not be happening and definitely sounds like a serious bug to me.


We will have to rebuild the chart widget sometimes, and updateDatasource is not enough.


I hope you will investigate this further and try to find a fix.


Kind regards,


Luca



YG Yuvaraj Gajaraj Syncfusion Team March 23, 2022 02:25 PM UTC

Hi Luca, 
 
Sorry for the inconvenience. 
We could replicate the memory leaks on calling setState(). So we will ensure and resolve the memory leaks occurring in all the scenarios, including all the chart types and will include the fixes in our upcoming 2022 Volume SP release. This is expected to be out at the end of April 2022. Please be patience until then. 
 
Regards, 
Yuvaraj. 



YG Yuvaraj Gajaraj Syncfusion Team March 27, 2022 04:04 PM UTC

Hi Luca, 
 
We have attached the feedback link below, you can track the status using this feedback. 
 
  
Regards, 
Yuvaraj. 



YG Yuvaraj Gajaraj Syncfusion Team May 16, 2022 02:55 PM UTC

Hi Luca,


The reported issue regarding memory leak while rebuilding the application has been fixed, and it is rolled out. To avoid this issue please upgrade the chart package to the latest version below.


Version: https://pub.dev/packages/syncfusion_flutter_charts/versions/20.1.55


Regards,

Yuvaraj.



LU Luca May 20, 2022 10:54 AM UTC

Hi Yuvaraj,


We have tried running our chart with syncfusion_flutter_chart version 20.1.56 and the problem is still persisting.




YG Yuvaraj Gajaraj Syncfusion Team May 23, 2022 05:15 PM UTC

Hi Luca,


In previous releases, when the data source in chart widgets is updated using the setState() for every 100 milliseconds the application will get crash due to a memory leak, this was because of the series-related information. Now we have resolved the memory leak which was due to a series related things and now the app will not crash while updating the chart every 100ms. Since we are not aware in which scenario the memory leak occurs, we request to share a few details that may be helpful in further analysis in providing the solution sooner.

  • Is the memory leak occurring due to any user interaction features like a tooltip, zooming, selection, etc.
  • Is the memory leak occurs
  • How many data do you have in the chart
  • If possible kindly share a screenshot of which class does the memory hold. For this, while taking snapshot, kindly clean the GC


Thanks,

Yuvaraj.


Loader.
Up arrow icon