DEV Community

Discussion on: Explain a "memory leak" like I'm five

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.