Skip to content

Need a tool to detect memory leaks in a flutter app #1437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
a-siva opened this issue Dec 5, 2019 · 9 comments
Closed

Need a tool to detect memory leaks in a flutter app #1437

a-siva opened this issue Dec 5, 2019 · 9 comments
Assignees
Labels
enhancement New feature or request screen: memory Issues with the Memory screen.
Milestone

Comments

@a-siva
Copy link

a-siva commented Dec 5, 2019

Here is the original text from @Aaron009 transferred from dart-lang/sdk#39645

========================================================================
I wrote a memory leak code.

import 'package:flutter/material.dart';

void main() {
  runApp(MainApp());
}

class MainApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: TextMemoryLeak(),
    );
  }
}

class TextMemoryLeak extends StatefulWidget {
  @override
  _TextMemoryLeakState createState() => _TextMemoryLeakState();
}

class _TextMemoryLeakState extends State<TextMemoryLeak> {
  List memoryList = [];

  @override
  Widget build(BuildContext context) {
    Scaffold scaffold = Scaffold(
      appBar: AppBar(
        title: Text('test memory leaks'),
      ),
      body: Center(
        child: Text('${memoryList.length + 1}'),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.map),
        onPressed: () {
          setState(() {

          });
        },
      ),
    );

    for (var i = 0; i < 100; ++i) {
      memoryList.add(scaffold); // A memory leak has occurred
      memoryList.add('A' * 1000);
    }
    return scaffold;
  }
}

This is a very simple code.

Every time you click on the floatingButton, it will be continuously added to the memoryList without being removed. This is an obvious memory leak.

I once read an article about Flutter checking for memory leaks.
The principle is that the larger the memory size requested by a function, the greater the possibility of a memory leak. (I forgot where I saw it. If anyone knows, please tell me the address of this article.)
I don't like this method because the memory requested by a function is very small, but it is a memory leak and it is difficult to check.

Another way is to use weakly referenced.

The principle is this.

var memoryList = [];
memoryList.add('111111');

var weakMap = new WeakMap()
weakMap.set(memoryList, true);

forceGC();

// According to the principle, after executing gc,
// if the memoryList key does not exist in the
// WeakMap,
// it means that the memoryList has recovered memory normally.
This troubleshooting method is very enjoyable. When I create an object, I make a weak reference to it to store it. When I destroy and force the use of GC, we can check the memory leaked objects through WeakMap.

There are several issues:
Dart does not have a weak reference. May I ask if there is a launch plan?
Does dart have a similar method to know if the object has been properly recycled?
In addition to the above two methods, is there a better way for Flutter to check for memory leaks?

I used to develop a game in which a colleague wrote a piece of code with a memory leak and then left. It was too painful when I investigated later.

@a-siva
Copy link
Author

a-siva commented Dec 5, 2019

/cc @Aaron009

@jacob314
Copy link
Contributor

jacob314 commented Dec 5, 2019

Fyi @terrylucas

@Aaron009
Copy link

Aaron009 commented Dec 5, 2019

@jacob314 @a-siva @terrylucas
I don't want to do tool checking because I haven't used very effective tools to check for memory leaks so far. I am a developer and have used many analysis tools, but it didn't help much.
This tool also has drawbacks, it takes a long time to observe and test, and may not be detectable.

I hope to have such a function.

In debug mode, an object is instantiated and then marked as recyclable by code. After GC was enforced, it was found that the object could not be recycled at this time, and an error or prompt was given.

@jacob314 jacob314 added this to the Backlog milestone Jan 8, 2020
@sgehrman

This comment was marked as abuse.

@britannio
Copy link

Is something similar to https://github.com/square/leakcanary possible in Dart/Flutter?

@terrylucas terrylucas self-assigned this Jul 31, 2020
@yuexunshi
Copy link

yuexunshi commented Oct 21, 2020

Is something similar to https://github.com/square/leakcanary possible in Dart/Flutter?

https://flutter.cn/community/tutorials/memory-leak-monitoring-on-flutter

This is the realization idea, it is not open source, if you have any ideas, please let me know

@kenzieschmoll
Copy link
Member

Once we have functionality to detect a memory leak, we should badge the memory tab with an error when a leak is detected.

@jacob314
Copy link
Contributor

jacob314 commented Jan 7, 2021

We can support this using the VM Service. Without the VM Service in the loop Dart's lack of weak reference support would get in the way.

@jacob314 jacob314 added the screen: memory Issues with the Memory screen. label Jan 7, 2021
@jacob314 jacob314 self-assigned this Mar 24, 2021
@polina-c
Copy link
Contributor

Duplicate of #3951

@polina-c polina-c marked this as a duplicate of #3951 May 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request screen: memory Issues with the Memory screen.
Projects
None yet
Development

No branches or pull requests

9 participants