Imagine you and your friends have some soccer balls. Every time one of you want to go play soccer, you get a ball, play with it, and bring it back. As long as everyone remembers to bring their ball back, you and your friends can play soccer as much as you want.
Now imagine you or your friends sometimes forget to bring a ball back. Eventually, all the balls get lost, and no one can play soccer anymore.
Memory in a computer is something all programs need to work. A memory leak is like when you forget to bring the soccer ball back. If a program doesn't put its memory back, then none of the programs can work anymore.
There are 10 plates in the house. Every time you have a meal, you pick up a clean plate but don't wash the plate afterwards. Soon you'll run out of clean plates. You'll have to go around the house collecting dirty plates and clean them before you can have your next meal.
Lol don't forget the part about being in the middle of the dark so you're walking around not knowing where the lego pieces are. That's the biggest part of memory leaks that is frustrating.
To extend the lego block analogy and make the explanation unnecessarily more complicated for applications running with managed memory models (e.g. C# and Java)...
Imagine there is an invisible robot vacuum cleaner (i.e. the garbage collector) that runs around automatically picking up lego blocks.
Nobody is really sure when the robot will run and what it will pick up.
Sometimes it will not pick up some of the blocks even when you know it has run (e.g. when memory is pinned) and sometimes the block could be collected but the robot just doesn't feel like picking it up on that run.
To frustrate you further, when you go looking for leaked blocks, sometimes they are chained together and the root block actually stopping a bunch of other blocks from being collected is a block you least suspect.
I forgot to mention that it is also a Heisenberg robot so when you go looking for leaked blocks, the robot starts behaving differently.
So in conclusion, not only do you trip over the blocks in the dark but you might get super lucky and trip over the invisible robot vacuum too!
I couldn't come up with an explanation for a 5 year old, but here's one for a baby:
You have 2 hands. If you want to pick up something you have to make sure one of your hands is empty. If you use up all your hands and forget to put something down, then you won't be able to grab things to put in your mouth.
Your computer has many many hands, but if you forget to empty the hands you'll eventually run out.
OK. So you know how you like to watch Peppa Pig on Netflix? And you know when we go shopping I have to pay for the things we buy?
Well, I also have to pay for Netflix.
Unlike with the shopping, I have set something up that sends money to Netflix every month.
If I cancel Netflix, so that you can't watch Peppa Pig anymore, I would also have to stop automatically sending money every month to Netflix. If I forget to do that then I will start losing money, like how water leaks out of a hole in a bucket.
On a computer or phone or tablet, apps have to give a certain amount of memory for each thing you can see on the screen, and each part of each thing. When a thing goes away, like when you close your Peppa Pig game, then the app has to give the memory back so that other apps can use it, like how I have to stop paying for Netflix if we no longer want to use it. If the app doesn't give the memory back then eventually the computer will run out of memory and no apps can run, like if I forgot to stop paying for things we would run out of money and not be able to buy anything else.
Each time you make food, you grab a clean plate out of the cupboard. At some point, there is an expectation that the plate will be washed and put back in the cupboard (maybe it's you who does it, maybe it's someone else's chore). You can think of the garbage collector in most programming languages as the dishwasher or housekeeping robot automatically cleaning dirty dishes for you as long as you put them in a place that the dishwasher/housekeeping robot can deal with them.
But let's say you forget to put them in a place your housekeeping robot can't find them. For example, you leave them piled up on your desk because you keep eating lunch at your desk. The next time you go to the cupboard, you're out of clean plates. You don't have anything to put your food on, so you can't make food at all. You can borrow plates from your neighbors (kiiiinda analogous to swapping memory to disk), but it takes a lot longer to go borrow a plate every time you need one and if you don't bring them back they'll either stop lending them to you (you hit the swap limit) or they run out of plates, too (disk fills up). You could also keep buying new plates (restarting the process) as a stopgap, but that becomes infeasible over time.
The ideal solution is to figure out why you're hoarding the plates and come up with a better routine to make sure you take the plates back to the kitchen and put them in the dishwasher so you'll have clean plates when you need them.
Simply, it's like writing on a white board with permanent marker. The white board is your precious memory space which you want the most possible of, but there are permanent marks from wrongly using your white board. You could try to even take these marks off, but you're ib a dark room where you don't actually see your white board and marks. ALL you know is that you have a white board, and a choice to use either the proper marker and erase, or permanent msrker and attempt to erase.
Coding for 20 years | Working for startups for 10 years | Team leader and mentor | More information about me: https://thevaluable.dev/page/about/
Twitter: @Cneude_Matthieu
Let's imagine that you started your washing machine before you went in holiday in an exotic country. Unfortunately, your washing machine is leaking and the room begins to fill with water. When you come back to your holidays, your house is now a swamp. Congratulation!
The washing machine is your code, the leaking part is what you need to fix, the water filling the room is the data filling the memory of your computer which will slow down, like you walking in your swamp.
You're playing Valheim, and you keep randomly picking up items until you're encumbered. You later realize you picked up many greydwarf eyes over the course of a few hours and that's all that is in your inventory. You need to stop picking up greydwarf eyes and try to get rid of most of the eyes you've collected.
Top comments (26)
Imagine you and your friends have some soccer balls. Every time one of you want to go play soccer, you get a ball, play with it, and bring it back. As long as everyone remembers to bring their ball back, you and your friends can play soccer as much as you want.
Now imagine you or your friends sometimes forget to bring a ball back. Eventually, all the balls get lost, and no one can play soccer anymore.
Memory in a computer is something all programs need to work. A memory leak is like when you forget to bring the soccer ball back. If a program doesn't put its memory back, then none of the programs can work anymore.
There are 10 plates in the house. Every time you have a meal, you pick up a clean plate but don't wash the plate afterwards. Soon you'll run out of clean plates. You'll have to go around the house collecting dirty plates and clean them before you can have your next meal.
Every night, you take off your clothes and throw them on the floor. You don't clean them up, you don't put them in the hamper, they just pile up.
Five years later, you have no clean clothes left to wear, and your room is full floor-to-ceiling with dirty clothes.
You are now sad. And now you'll need to go to your task bar and kill your Room process. There go all your unsaved toys.
Lol don't forget the part about being in the middle of the dark so you're walking around not knowing where the lego pieces are. That's the biggest part of memory leaks that is frustrating.
To extend the lego block analogy and make the explanation unnecessarily more complicated for applications running with managed memory models (e.g. C# and Java)...
Imagine there is an invisible robot vacuum cleaner (i.e. the garbage collector) that runs around automatically picking up lego blocks.
Nobody is really sure when the robot will run and what it will pick up.
Sometimes it will not pick up some of the blocks even when you know it has run (e.g. when memory is pinned) and sometimes the block could be collected but the robot just doesn't feel like picking it up on that run.
To frustrate you further, when you go looking for leaked blocks, sometimes they are chained together and the root block actually stopping a bunch of other blocks from being collected is a block you least suspect.
I forgot to mention that it is also a Heisenberg robot so when you go looking for leaked blocks, the robot starts behaving differently.
So in conclusion, not only do you trip over the blocks in the dark but you might get super lucky and trip over the invisible robot vacuum too!
I couldn't come up with an explanation for a 5 year old, but here's one for a baby:
You have 2 hands. If you want to pick up something you have to make sure one of your hands is empty. If you use up all your hands and forget to put something down, then you won't be able to grab things to put in your mouth.
Your computer has many many hands, but if you forget to empty the hands you'll eventually run out.
OK. So you know how you like to watch Peppa Pig on Netflix? And you know when we go shopping I have to pay for the things we buy?
Well, I also have to pay for Netflix.
Unlike with the shopping, I have set something up that sends money to Netflix every month.
If I cancel Netflix, so that you can't watch Peppa Pig anymore, I would also have to stop automatically sending money every month to Netflix. If I forget to do that then I will start losing money, like how water leaks out of a hole in a bucket.
On a computer or phone or tablet, apps have to give a certain amount of memory for each thing you can see on the screen, and each part of each thing. When a thing goes away, like when you close your Peppa Pig game, then the app has to give the memory back so that other apps can use it, like how I have to stop paying for Netflix if we no longer want to use it. If the app doesn't give the memory back then eventually the computer will run out of memory and no apps can run, like if I forgot to stop paying for things we would run out of money and not be able to buy anything else.
Each time you make food, you grab a clean plate out of the cupboard. At some point, there is an expectation that the plate will be washed and put back in the cupboard (maybe it's you who does it, maybe it's someone else's chore). You can think of the garbage collector in most programming languages as the dishwasher or housekeeping robot automatically cleaning dirty dishes for you as long as you put them in a place that the dishwasher/housekeeping robot can deal with them.
But let's say you forget to put them in a place your housekeeping robot can't find them. For example, you leave them piled up on your desk because you keep eating lunch at your desk. The next time you go to the cupboard, you're out of clean plates. You don't have anything to put your food on, so you can't make food at all. You can borrow plates from your neighbors (kiiiinda analogous to swapping memory to disk), but it takes a lot longer to go borrow a plate every time you need one and if you don't bring them back they'll either stop lending them to you (you hit the swap limit) or they run out of plates, too (disk fills up). You could also keep buying new plates (restarting the process) as a stopgap, but that becomes infeasible over time.
The ideal solution is to figure out why you're hoarding the plates and come up with a better routine to make sure you take the plates back to the kitchen and put them in the dishwasher so you'll have clean plates when you need them.
Simply, it's like writing on a white board with permanent marker. The white board is your precious memory space which you want the most possible of, but there are permanent marks from wrongly using your white board. You could try to even take these marks off, but you're ib a dark room where you don't actually see your white board and marks. ALL you know is that you have a white board, and a choice to use either the proper marker and erase, or permanent msrker and attempt to erase.
Let's imagine that you started your washing machine before you went in holiday in an exotic country. Unfortunately, your washing machine is leaking and the room begins to fill with water. When you come back to your holidays, your house is now a swamp. Congratulation!
The washing machine is your code, the leaking part is what you need to fix, the water filling the room is the data filling the memory of your computer which will slow down, like you walking in your swamp.
You're playing Valheim, and you keep randomly picking up items until you're encumbered. You later realize you picked up many greydwarf eyes over the course of a few hours and that's all that is in your inventory. You need to stop picking up greydwarf eyes and try to get rid of most of the eyes you've collected.