Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this memory leaking?

Tags:

flutter

In this example, each time I press "Click", 50M of ram is allocated. It is never reclaimed, I can push 30 pages and take up 1.5gb, despite there only ever being 1 page on the nav stack. GC never kicks in. What's going on here?

Flutter (Channel master, 2.1.0-11.0.pre.122, on Microsoft Windows [Version 10.0.18363.1440], locale en-US)

void main() {
  runApp(MaterialApp(
    home: MemoryTest(),
  ));
}

class MemoryTest extends StatelessWidget {
  final List<EdgeInsets> insets = List.generate(
    1000000,
    (index) => EdgeInsets.all(0),
  );
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          OutlinedButton(
            child: Text("CLICK"),
            onPressed: () {
              Navigator.of(context).pushReplacement(MaterialPageRoute(
                builder: (_) => MemoryTest(),
              ));
            },
          ),
        ],
      ),
    );
  }
}
like image 409
shawnblais Avatar asked Apr 02 '21 01:04

shawnblais


People also ask

What causes a memory leak?

A memory leak starts when a program requests a chunk of memory from the operating system for itself and its data. [ Keep up on the latest thought leadership, insights, how-to, and analysis on IT through Computerworld's newsletters. ] As a program operates, it sometimes needs more memory and makes an additional request.

Is memory leak serious?

Very dangerous. Memory leaks in the kernel level lead to serious system stability issues. Kernel memory is very limited compared to user land memory and should be handled cautiously. Memory is allocated but never freed.

What is memory leak?

What is Memory Leak? How can we avoid? Memory leak occurs when programmers create a memory in heap and forget to delete it. The consequences of memory leak is that it reduces the performance of the computer by reducing the amount of available memory.

What causes memory leaks in Windows 10?

This may be caused by memory leaks. Memory leak refers to the RAM memory loss in Windows due to an app or program. When opening the Task Manager, you may find a certain app is taking much memory, which leads to slow running. Sometimes, you cannot deal with even the easiest tasks in Windows.

How to avoid memory leaks in C++?

To avoid memory leaks, memory allocated on heap should always be freed when no longer needed. Memory leak in C++ and How to avoid it? Memory leak in C++ and How to avoid it?

Can a memory leak slow down a computer?

Modern computers are equipped with a lot of storage space and memory and are quite fast in their operations. But sometimes, a small problem here or there could lead to the slowing down of a computer. One of the main reasons for this is a memory leak. But what exactly is a memory link, and how does it happen?


Video Answer


1 Answers

The answer here is twofold. On one hand, its a bug, and the flutter team is working on it: https://github.com/flutter/flutter/issues/79605

The actual cause for the bug here seems to actually be the closure, wrapping context which is forcing the stateless widget to stay in memory forever.

If you avoid the context in the closure, by caching the navigator as a local var, the leak doesnt happen. Obviously most Flutter devs are not doing this, and many code examples directly from Flutter also do this, so hopefully the team can fix this.

like image 126
shawnblais Avatar answered Nov 10 '22 20:11

shawnblais