DEV Community

loading...
Cover image for Explain a "memory leak" like I'm five

Explain a "memory leak" like I'm five

Peter Kim Frank
Doing a bit of everything at DEV.
・1 min read

Cover Photo via Unsplash

Discussion (28)

Collapse
lukeshiru profile image
LUKE知る

Is like when you play with lego bricks, and you leave some lying around, and daddy later can't walk around the house without stumbling. So the house is the memory, you are a badly written piece of code, the lego bricks are pieces of memory that you didn't let go, and I'm other pieces of code that can't run properly because of you.

PS: Nothing like calling a 5 yo "badly written piece of code". I will be a great father in the future 🤣

Collapse
jwhenry3 profile image
Justin Henry • Edited

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.

Collapse
glsolaria profile image
G.L Solaria

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!

Collapse
zkwsk profile image
Zaki Wasik

But "daddy not being able to walk around the house" is an externality. It's something caused by the 5 year old, but it is daddy who takes the cost. My 5 year old self would consider a house with floors covered in Lego the absolute ideal place to be.

Collapse
kjwong3 profile image
Kelly Wong • Edited

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.

Collapse
eljayadobe profile image
Eljay-Adobe

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.

Collapse
arvindpdmn profile image
Arvind Padmanabhan

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.

Collapse
citizen428 profile image
Michael Kohl • Edited

It's like your room: if you leave things lying on the floor, you have less space left. So the next thing needs to go somewhere else until there's no floor space left at all.

You can think of the leaking part like an actual leak: one of the things you left lying in your room was an open container of orange juice. The juice that spills out is taking up more and more floor space, even though you're not putting new things there by yourself. But the result is the same, at one point there will be no more clean floor space to put more things on.

Collapse
aminmansuri profile image
hidden_dude • Edited

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.

Collapse
mellen profile image
Matt Ellen

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.

Collapse
giorgikobaidze profile image
Giorgi Kobaidze

If I was a generic "Stack Overflow Genius" I would just tell you (as a 5-year-old kid) to read more about that and then I would ban you from Stack Overflow for good.

But let's not be them and let me explain it with your toy dump truck (some other process, that is using the memory). So you've got to deliver a sand (memory) from backyard to front yard where you need to build a house (the target task) with your truck which is so cool that it even has a container that you can open and close. You filled that up and went straight to the front yard with a full throttle. Everything seems to be right, doesn't it? Well... not exactly, cause as soon as you arrive, you'll find out that there is about half of the sand in the container, because you forgot to close it. The remaining sand is all over your yard. Now you can't build the house, because there isn't enough sand. What a shame...

But anyway, lesson learned. From this moment on you will always make sure that you closed your container tightly, so the sand won't escape anymore. The same happens in Software Development, you learn on mistakes. Now go and play with your dump truck.

Alt Dump Truck

Collapse
phantas0s profile image
Matthieu Cneude

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.

Collapse
jwhenry3 profile image
Justin Henry

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.

Collapse
jwhenry3 profile image
Justin Henry

Cars can only go as fast as they can if they aren't carrying much weight. If you keep adding weight to the car, it will move slower. A memory leak is like that, and in many cases, you can't figure out where all the extra weight is coming from.

Collapse
nombrekeff profile image
Keff

Imagine you have a pool, but it has holes in it, and after a while the pool empties itself, so there is no more water for you to swim in or other people to swim in. No matter the times you fill it, it will always empty, until you fix the holes.

Collapse
nadinfromc137 profile image
Nadin H. ElShanawany

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.

Collapse
jgaskins profile image
Jamie Gaskins

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.

Collapse
siddharthshyniben profile image
Siddharth

Imagine the computer is a hotel, and programs are people coming to the hotel, and the rooms are memory. So, programs rent rooms (memory) and use them. Later, they can leave the room (memory) so that others can use the room (memory).

A memory leak happens when an person (program) leaves the room without telling anyone. Now, that room (memory) is locked there, and others cannot use it because the hotel thinks that someone else is using it. This is a memory leak.

Collapse
siddharthshyniben profile image
Siddharth

Some OSs are smarter than others, and figures out that memory is not used and clears the memory and kicks out your program anyways. But some OSs like Windows, just keep asking 'do you want this memory anymore?'.

Collapse
amit2908 profile image
amit2908

When two person become strong friends of each other and none of them wants to go away from each other. Then they reserve some time and space for each other, which is protected by both of them.
To break their friendship you have to make one of the friends as weak friend, so that the relationship between them becomes weak from one side and can be broken.

Collapse
qainsights profile image
NaveenKumar Namachivayam ⚡

Holding up all your toys, even though you are not playing with it, and also not sharing with the other kids. :)

Collapse
nombrekeff profile image
Keff

Imagine you have a balloon filled with water, but it has a hole in it that you don't see at first. Then you go throw it to your sister, and it's empty.

Collapse
hasnaindev profile image
Muhammad Hasnain

You're not five ;_;

Collapse
sinewalker profile image
Mike Lockhart

A memory leak is like a five-year-old who won't clean up their room.

If you don't clean up your room, you will never find anything, and your room will also be to cluttered to dump anything more.

Collapse
kampouse profile image
Kampouse

Its like leaving lego blocks but daddy cant pick them up and they keep stacking up until you cant walk 🥲

Collapse
jaymales profile image
Jay

A memory leak is like eating food that you can't poop out. You keep eating it until you it fills you up completely and explode.

Collapse
bmrussell profile image
Brian Russell

Walking along putting stones in my pockets that I'm going to use for something later. When my pockets get full it's real heavy and I fall over.

Collapse
tonnytg profile image
Antonio Thomacelli Gomes

When you have a car, and you run 999.999miles when you complete more than one mile, you leak... 000.001