Majid Hajian

Let’s Talk About Memory Leaks In Dart And Flutter

Transcript

Majid

All right, so welcome back after lunch. Oh, wow. This is... I don't see you. I am serious. I don't see you. Just this row I see. So... After lunch is two slots for public speaking is the worst. One before lunch, one after lunch. Both are terrible. So we need to make you entertain, not just give a talk, to make you awake for the next 30 minutes and hopefully the rest of the conference. And I think that's the reason they put me in this slot, because they think I'm funny. I'm not, of course. And also, we did something last two years. I've been here two years ago, same stage, and some of you probably have been here. And actually, we did something interesting. Very ad hoc, very random, I think, even organizer, including also myself. I'm not an organizer, but I was surprised how well it worked. I brought two socks and I just gave it to one person from this row and one person from that row. And I asked during the talk... just pass these socks to your, like, whatever you see, just pass it to the next person. And if we get to the last one, then again, do it the reverse, back to the first. Just do it like that until I say one word. You need to listen to me very carefully. When I say that word, then whoever gets the socks just needs to be standing as fast as possible and shout as loud as possible. Let's agree on which word you need to say. Let's say, you can say flutter, connection, very loud, like just scream. Okay. If you do that, you'll get the socks. Not only that, one of the socks has a very special thing in it. So, hand it over to my personal assistant. If that person opens, please do not open and look at the inside. Like, be very honest. Do not do that. And if that person opens the socks and finds that surprise, will get my book for free as well. Take it away home. So... I'll do it here so you get inspiration. It's 600 pages it's supposed to stand. That's the reason I wrote 600. So now let's get started. I have actually a packed agenda already five minutes past. So that was not part of the talk. But I want to talk about memory leaks. And I will set some expectation, of course, for you, what you will get from this talk. And hopefully after this talk, I hope I will not see those... kind of issues anymore in any Flutter codebase. Please pay attention. These are simple stuff that you very simply overlooked and then you get a problem at some point. You probably even have seen, maybe one of you have written this in even Reddit right now. Oh, my app is gradually consuming too much RAM. Oh, it crashed. How the hell is happening? Everything looks fine. What should I do? And you look at your code, you actually see everything is fine. You literally see everything is fine, right? But I'm going to pass this one because I was introduced anyway. And that's what we're going to talk about it today. Because we need to talk about Dart VM a bit and how garbage collection works. It's going to be too high level, but I will have multiple QR codes today. Please bring up your phone, scan them, and check them later. And that's important. Check them later. But scan them, keep the links, because there are multiple links that you can go deeper and understand even more. I'll go through the Dart VM for sure a bit, so we get the same understanding. And we understand why you get those consumption in the memory, even if you think everything is fine, I'm not on fire. Sure. And then let's have some samples and some, the most kind of issues that I have seen in the last six, seven years of working with Flutter and on multiple different code bases. And I'll give you some also tools so you can hopefully utilize them to figure out. And obviously some best practices. This is expectation. Okay. Let's clarify one thing. So is when we're talking about Flutter memory, we're talking about both Dart layer in the architecture and the native layer. Obviously, we're not handling the native one for now. We're not even talking about it right now. So we're going to focus on the Dart and Dart garbage collection. So let's focus on that, but I want you to explore more on the native part and you will find some links on it as well. Let's clarify one thing here. There are two different misconceptions, misunderstandings, let's say, about memories when I talk to different developers. So one is about memory bloating and another one is memory leak. When we're talking about memory leaks is a kind of problem that happens to the memory or in the hip, I say what it is, where gradually you're going to consume more and more because the garbage collection cannot free up the object that you created. Right. And we will see how it happens later. in a minute, but then memory bloating is something that you deliberately do it. Maybe you're loading an image into the memory for caching purposes, for example, or you're loading your JSON into the memory or things like that, which suddenly you consume more memories, right? Both of them can degrade your performance or your application's performance. it's important that you clean up at least for memory bloating and also very important for memory leak. However, the memory leak sometimes happens without you noticing because you think garbage collection is taking a good care of you which it does but there are some missing parts. Okay, I'll continue until we have the slides. But what happens is that the dart actually comes with garbage collection and what type of garbage collection it has, it's called generational garbage collection. What the hell it means For us, I simplified it for you. You're not supposed to just read everything here. You're supposed to listen to me, and it will support my wording. You can take a look later. What happens is that when an object is created, so there are two type of kind of generation here. So one is the young, which takes care of the nursery. As soon as you create an object and there is no reference and no use, so the garbage collection will take care of that and will remove it from the memory and free up the memory. The second one though is for long-lived object. So the object that you create and it lives in the memory for long in time. And then it goes to the garbage collection. Garbage collection uses an algorithm called mark and sweep, which technically means, hey, I have an object. There's no reference. Mark it for garbage collection. Then go to sweep, remove it, and then free up my memory. Just simplified it here in this slide, you can take a look later. But essentially, I will quickly explain it here. You create an object, it goes into the Dart heap or the heap. Heap is a host for your object. just in the memory, okay? So then you move on, you're going to have a root object. Whatever object you create is going to be the root. So everything then afterwards will get a reference to that. And what happens is that then... your garbage collection takes, take a look at these, at garbage collector, take a look at the references and say, okay, the root, the object that you created, will either have some kind of reference or it doesn't. If it has some kind of reference, then it actually will be remained or retained, the correct word, in the memory. But if it doesn't, then it will go and be sweeped away or the memory will be freed. So, okay, so why does this matter? Because these are actually very nice when you create something that does not have reference. If you unintentionally create an object that keeps the reference in the memory, garbage collection cannot free up the memory for you because there is a reference, even though you don't need that object. Okay, to simplify the idea, here is just a diagram that shows you. Here's the roots and then I'll go through the references. The blue ones are the ones that are not connected to the root. Therefore, they're going to be freed up. They're going to be garbage collected. If you want to learn about the garbage collection or GC in Dart, I strongly recommend you to watch Anna's talk in Flutter Vikings 2022. It's going way deep to the Dart VMs and show you how exactly this works. I just gave you two minutes overview, but it's way more than this. Now we must be happy. And this is AI generated. Disclaimer. Actually, this is fascinating. I have not touched the AI generated image at all. Those fonts, logo, everything is generated by the latest model of GPT, which is fascinating.

Edit on GitHub