– Computer Memory Leaks. Why do they happen, and what happens?

570 views

– Computer Memory Leaks. Why do they happen, and what happens?

In: Technology

The classic memory leak occurs because of a bad memory cleanup routine.

An application stores data in RAM by definition, but when it’s done with that RAM it has to release it allowing it to be used by something else.

So let’s make up an example. You have an application that has an undo option, this allows you to undo the very last thing you did in the application. But in order for this to work the application has to track the last thing you changed so it can undo it, this is stored in memory.

Now imagine the developer forgot to write the piece of code that clears the previous actions from memory, so instead of just storing the very last thing you did, it stores every single thing you’ve done since you opened the application. Even though you can only undo the very last one.

These actions keep building up in memory until there’s nothing left for other applications to use.

Think of computer memory as a shed to store things in. When you want to add stuff to storage, you place a plastic bin of a certain size into the shed and put a label on it. You then put items into the bins based on the label. The bins prevent the contents from flowing into unintended areas and the labels tell you that it is in use.

When you are done with the contents of a bin, you remove the label. Periodically, you’ll go through the shed and remove all bins without labels.

A memory leak happens when you forget to remove the label off of bins you don’t need. As you keep adding labelled bins but don’t remove the labels to free up the space, you use up more and more memory until you run out of storage space.

This typically occurs when coders allocate memory space but forget to free up the space when they are done. In certain coding languages this must be done manually with separate lines of code.

Give me an apple, please. OK, here, take the apple back.
Give me an apple, please. OK, here, take the apple back.
Give me an apple.
Give me an apple, please. OK, here, take the apple back.

That’s a memory leak. I (the program) asked for some memory, and when I’m done with it I give it back. But sometimes I forget to give it back. There are many, many very obscure ways this can happen.

This is a game of kid musical chairs, but a kid that keeps leaking everywhere (possibly eaten a bad apple) that kid sits makes a mark on the seats taken. Those seats aren’t cleaned up in the middle of the game, nor does the kid clean up after the seat sat in and this makes other kids not want to sit in those seats in contaminated sitting. To the kids that do sit on those dirty seats get the mess the leaking kid left behind. A teacher can take away the dirty seats in the middle of the game but is not able to return those cleaned seats back to the game in time.

The kid leaving the leak obviously has a digestion compilation problem (problem application).

The seats in the circle are the available physical memory space.

The teacher, if there is one, is the operating system.

If the kid leaking cleans up the seat after every sit, then that would be where an application has a proper application cleanup in development.