Open Clon1998 opened 1 year ago
I’m pretty sure it’s because of the static cache as I said here. The ‘LineChartHelper._cachedResults’ should generate the leak maintaining references to the lists and so the FlSpots.
It might be the case I didn't look too much into this since I managed to find a workaround. However, I will have a look at the code you mentioned.
Sorry I’m on holidays and I don’t have the time nor the device to check this but could you retry your example having the memory leak but with calling a clear cache on the LineChartHelper each time you change the data values. Your will need to add this clearCache static method just clearing the _results map. If it is what I think it is, it should ‘fix’ your leak and we will need to refactor this. And already thanks for your help and early investigation !
I’m pretty sure it’s because of the static cache as I said here. The ‘LineChartHelper._cachedResults’ should generate the leak maintaining references to the lists and so the FlSpots.
Dear @imaNNeo What is the solution?I see same problem in memory, so that while I close my chart screen, but memory filled by chart data is not cleared.
I’m pretty sure it’s because of the static cache as I said here. The ‘LineChartHelper._cachedResults’ should generate the leak maintaining references to the lists and so the FlSpots.
Dear @imaNNeo What is the solution?I see same problem in memory, so that while I close my chart screen, but memory filled by chart data is not cleared.
Fixed in the main
branch.
I will notify you here when we released a new version.
Fixed in 0.66.1, please test and let me know if the issue is fixed.
Thanks for your patience!
Thank you dear @imaNNeo I will try it and let you know the result here.
I'm using lineChartData, but I'm getting a Platform_enviroment error after upgrading the version. The platform currently used is web.
I'm using lineChartData, but I'm getting a Platform_enviroment error after upgrading the version. The platform currently used is web.
This is another issue related to #1565 Please test the memory issue in the next release
Guys, this memory issue should be resolved on 0.66.1. Can you please validate it in your apps?
I'm on 0.68.0 and still have FlSpots taking up multiple Gigabytes after a few hours. Can't confirm if the cause is the same as described here.
same problem here...I can solve the problem by not using the built-in "min" / "max" autocalculation and doing it myself. I will be more precise soon with some code.
The root cause of this issue is that _cachedResults
does not have a reliable cleanup mechanism. If you think providing a isEnableCache
option is bad 1283 at least add a time limit or size limit?
Thanks for sharing the information. As we don't have any quick solution to fix the issue, I will disable the caching for now. The trade off is that we need to calculate the min, max values based on the data (if it is not provided) in every frame. I wrote in the documentation that it is more performant if you provide these parameters.
Btw, for the final fix (resolve the performance issue if you haven't provided the parameters), we need to move the calculation logic into the renderer objects as they remain in the memory as long as widget exists. So this way, we don't need to calculate the values in every frame. We just keep and calculate them once the data is changed in the renderer objects that we have. I have created #1693 to allow you to follow up the further improvements
Describe the bug When chaning the complete FLSpot list instance/reference e.g. calculating it based on another list whenever the list changes the
LineChart
is leaking a ton ofFlSpots
Here is a DevTool-measurement within seconds the Spots grows to 100k+ instances ( Example code used to measure can be found below)
To Reproduce To debug the behavior I had a look at the Sample#10 which does not leak any memory. However, I edited and also managed to reproduce the FLSpot leaking I encountered in my own app. Instead of using an instance of
final sinePoints=<FlSpot>[]
(src) I am using afinal sinPoints = <double>[];
list which I later map toFlSpots
using the_convertToPlotSpots
method. Also to verify that the issue is caused by the chart I replaced theLineChart
widget with anText('Test:${_convertToPlotSpots(sinPoints).last.x}');
widget that uses also the FlSpot list just to see if the leaking is caused by the code in_convertToPlotSpots
which is not the case. Finaly I found a workaround for me by using a single instance ofList<FlSpot>
that does not change/only swapps elements using this code for the_convertToPlotSpots
method:Sample#10 leaking Memory:
Flutter 3.0.3 • channel stable • https://github.com/flutter/flutter.git Framework • revision 676cefaaff (5 weeks ago) • 2022-06-22 11:34:49 -0700 Engine • revision ffe7b86a1e Tools • Dart 2.17.5 • DevTools 2.12.2
fl_chart: ^0.55.0