且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Flutter中查找内存泄漏?

更新时间:2023-02-12 14:11:27

Am在android studio ide中实现了内存泄漏测试。

Am implemented Memory leack testing in android studio ide.

步骤-1:
将设备与android studio连接,然后在设备上运行应用程序。

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

步骤-2 :
转到视图->工具窗口-> Flutter性能

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

步骤-3:
窗口底部的打开开发工具选项将在那,点击它。它将导航到浏览器的新窗口。
有关更多详细信息,请参见下图:

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 :


步骤4:
要按照屏幕快照执行以下步骤,您可以查看对象的大小和详细信息。这是造成内存泄漏的原因。
首先从可用菜单中选择内存,然后在下面的ui中看到。

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.

 1. Click on settings icon

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

 3. Click on "Apply" button.

步骤-5:
这是最后一步,现在您可以看到内存泄漏信息。

Step - 5 : This is final step, now you can able to see memory leaking info.

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.

步骤6:
您可以使用 GC手动调用垃圾收集器。您可以随时使用重置和快照按钮重置并获取最新的快照。

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