Hello and welcome to another episode in the Godot basics tutorial series in this episode we will be taking a look at memory leaks. To understand memory leaks,
Let's go over a brief and simplified look at garbage collection. Now garbage collection is a form of automatic memory management. A lot of programming languages support garbage collection. Some of those include Python, Java, and Ruby. Along with many others now there are different types of garbage collection implementations and different languages can support different types of garbage collection implementations. For example Python
As you may know has reference counting. Let's move on now. Garbage collection has its uses and it comes with its own advantages and disadvantages. Now there are a lot of advantages and disadvantages that the garbage collection comes with. However I do want to limit the advantages and the disadvantages to use cases that will apply to Godot. So first let's start with the disadvantages. Now disadvantages of using garbage collection is that it impacts performance as in your code base in a sense will run slower. On top of that garbage collection does consume resources from your computer
Because even though you are not implementing garbage collection, under the hood the language is and it needs to allocate some resources in order to perform the process of garbage collection.
And the last case is that that in some cases your program can freeze in order to give your garbage collection time to execute. And this is probably the most important reason why Godot doesn't have garbage collection and that's something I've explained in other videos. Godot doesn't have garbage collection. Godot makes you handle it manually and these are three reasons I believe that Godot chose not to go this route. In a sense I'm not sure. However one big reason - And we'll take a look at this in the next slide -
Is that your game could potentially freeze. And when you're playing a game you most likely do not want that.
Now there are advantages to garbage collection and the only one I want to focus on is that it fixes certain memory leak issues in which a program fails to free unreachable memory. As a matter of fact that's one of the harder things to understand with Godot, And that's because most programming languages, And when you're working with Unity, handle garbage collection for you under the hood however Godot does not do that. However, Garbage collection is a problem. For example Unity uses garbage collection and yet unity has an entire web page dedicated to the issues of garbage collection in the unity eco system and they provide different tutorials and articles explaining how to improve unity performance by reducing the amount of time garbage collection takes.
In this case how long it takes with the unity. Moving on. So to reiterate Godot does not have garbage collection. We have to manage memory ourselves and as matter of fact because we have to manage memory ourselves. There is a high likelihood of memory leak issues you may not be aware of. And basically the only memory leak issues we will face is when our Godot application fails to free memory before it becomes unreachable. Now let's go ahead and look at a common example of Memory leaks.
So here we just started our game. We have a simplified memory address. And in this case our game can only hold Three separate objects. We're gonna use sprites as an example. And. At the bottom we have our scene. And in this case. Our scene has nothing on it. Zero. Now let's go ahead and add the sprite could go onto our scene. Now I went ahead and added Godot onto our scene and as you can see we have one in this case sprite node on our scene and it is in fact active.
And so in the last episode I tried to show you with code but in this episode I'm going to show you through images. So we add our sprite onto scene and let's go ahead and look what happens to our memory address when we remove it from the scene. So in this case now we've deleted Godot from our scene as you can see we're not pointing to the scene anymore. Our scene has zero nodes in this case Sprite and our location a memory address which was once marked active. Let's go back to the previous slide as you can see here it was marked active. Now we see that it is inactive.
As a matter of fact we call them orphan nodes. And at this point in time our node. Is unreachable. There is no way to access it. At this point in time. Also keep in mind that we did not call the queue free or free method. As I showed you in the last episode. Well let's move on. Let's go ahead and add two more sprites, two more Godot sprites onto the scene and as you can see here a scene has two sprite nodes. However our memory address is four. And now let's delete all the notes from the scene. And as you can see here we have a Full memory address.
There's no more memory that we can add and occupy in our space. And this is a problem. This is what we call a memory leak when we are unable to do anything about the fact that our memory is continually growing. Another word for a memory leak is also memory exhaustion. And this is the main problem that we face when we have to manually manage memory. However the solution is fairly straightforward. So let's go ahead and look at that. As you can see here the first technique is using queue free or free method.
And I showed you in the last slide to put that in the exit trees so you don't have to worry about calling that inside code rather when a node leaves the scene it will delete itself from memory and as you can see here in this example our queue free was inside the exit tree just like I showed you in the last episode. And while it's gone we have a clean memory slate. Now there is another technique I want to
Show you so we'll go over the theory. I will not show you any code. And that is a technique called Object pooling. And it's a very common technique in game programming as a matter of fact I believe Unity actually explains this in their Website and videos as well as
One way to keep unity performant. And if you're coming from unity you can Use this technique in G.D. script as well. So let's go over the theory. So we have an active node. We have our Godot which is our sprite node and it is on the scene. But now we're going to create a variable. Obviously this variable is going to be in a nother script probably on a node that's acting as a manager. However this variable. Will point to our good dose sprite note which are a good dose spread is on the same. And by scene I just mean it's drawn onto the screen
For you to see the image. Now let's go ahead and delete Godot. And as you can see there is no drawings on our screen however our variable which is attached again to a different node that's acting as a manager Class is in fact pointing to our now orphaned node. And then when we want to reuse our sprite we can in fact use the ADD child from the get root, Excuse me, get tree get root methods. And from there we can re add our node back on to the scene.
And as a matter of fact object pooliing is more performance than using queue free or the free method. And that's because it takes time To Instantiate and delete an object from memory. Just keep that in the Back of your head that object pooliing is a technique that we can use to increase performance even though we're manually managing memory. Well that's all I have for you in this episode. Thank you so much for joining me. If you have any questions or comments please feel free to leave them in the comments section down below.
Thank you so much for liking and thank you so much for subscribing. I look forward to seeing you in the next step episode. Have an amazing day.