How to find memory leaks in Flutter?

Issue

I need to find memory leaks in Flutter.
How to find them? and how to create memory leaks for the test?

Solution

Am implemented Memory leack testing in android studio ide.

Step – 1 :
Connect your device with android studio and run your application on your device.

Step – 2 :
Go to View -> Tool Windows -> Flutter Performance

Step – 3 :
Bottom of the window "Open Dev Tools" option will be there, click on it. It will be navigate into new window of your browser.
See below image for more details :

enter image description here
enter image description here

Step – 4 :
To follow below steps as per screen shot , you can able to see object size and details.Which are caused memory leakages.
First Select "Memory" from available menus than you can able to see below ui.
enter image description here

 1. Click on settings icon

 2. Select "Dart" and "Flutter"  from checkbox.

 3. Click on "Apply" button.

Step – 5 :
This is final step, now you can able to see memory leaking info.
enter image description here

1. Click on "Snapshot" it will be collect and display object list in bottom of the window.
2. Click on search icon and Here you can see those classes which objects are not destroyed. Suppose am selected "ApiRepository.dart" class and instance will be available in memory ,so that details are visible in window. If multiple objects created than you can see here the total no. of instance and total size.

Step – 6 :
You can able to call Garbage Collector manually by using "GC" . You can anytime Reset and get latest snapshot using "Reset" and "Snapshot" buttons.

For more information about Memory allocation related details read below articles :

https://medium.com/flutter/flutter-dont-fear-the-garbage-collector-d69b3ff1ca30
https://flutter.dev/docs/development/tools/devtools/memory

UPDATED: devtools/memory

Answered By – Sathish Gadde

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *