Memory Leaks

1.19K views

Memory Leaks

In: 661

25 Answers

Anonymous 0 Comments

I assume you’re talking about memory leaks in a computer. Memory ‘leaks’ when a program allocates memory (which means it can’t be used for anything else), then doesn’t deallocate it (freeing it up for others to use). Over time more and more memory will be marked as unusable for other programs until it’s all used up.

Anonymous 0 Comments

Your program says it needs some memory. When it’s finished using it, it doesn’t tell the OS it’s done with it… Then it asks for some more… And doesn’t give that back either. Eventually there isn’t anymore, cause it’s full of stuff that your program isn’t using but it never gave back to the OS

Anonymous 0 Comments

Imagine you have a group of buckets. You take a bucket so now there’s one less bucket available. You’re done with the bucket, but don’t put it back where it was before and you forget about it. You now need a bucket again, and instead of using the one you have, you take another one from the remaining buckets. You only remember the 1 bucket, but you never returned the last one so 2 buckets are missing from the group now.

Anonymous 0 Comments

When a program needs more RAM it asks the operating system

The OS goes “there you go here’s your X kB of RAM, they are at address Y”

The program has to always remember that address Y to know where the memory it needs is in order to use it

If it doesn’t need that memory anymore it can just tell the OS “you know that RAM at address Y you gave me? You can have it back now”

If a program forgets the address Y it has no way to use the ram and has no way to tell the OS to have it back, so that memory is leaked and can’t be used for anything until you close that program

Another way to leak memory is when the program is done with the memory and doesn’t use it, but forgets to tell the OS and asks for more next time it needs to do the thing

Anonymous 0 Comments

Let’s say you have a nice empty bookshelf. You go to the library, take out a few books, bring them home and put them on your bookshelf. You read all of them, and you just forget to pack one up when you go to return them. The book stays on your shelf. You go get more books from the library, you put them on your bookshelf, you read them, and then you forget another when you go to return them.

If you keep doing this, eventually, your bookshelf that started out as empty and shouldn’t have any books on it becomes full, and you don’t have any room to put the books you just got from the library.

A memory leak is similar. When writing in very low-level languages, you have to actually allocate memory, and then you have to release it when you’re done. If you forget to release memory, it just stays there locked by your application, but not doing anything useful. If this happens in some looping process, you will keep doing it over and over again until your system runs out of memory.

Anonymous 0 Comments

Suppose you have a bag of apples. I ask you to give me an apple, and you do. Then I give the apple back.

I ask for an apple, you give it to me, I give it back.

I ask for an apple, you give it to me.

I ask for an apple, you give it to me, I give it back.

Notice the difference? That’s a memory leak. When a computer program runs, it asks the computer for some memory, and when the program is done with the memory it tells the computer, so the computer can use it for something else. But, every once in a while a program won’t release the memory. The program just continues to hold on to the memory, and then asks for more.

This can happen for a variety of reasons, ranging from very simple (the program has a bug and just doesn’t release the memory) to super complicated (which are way beyond ELI5).

Anonymous 0 Comments

You have a sock drawer full of socks.Each day, you pull out a fresh pair of socks to wear that day.

When you get undressed for the night, you are supposed to put your dirty socks in the laundry chute. Your mom will wash these and put them back in your sock drawer.

But, sometimes you forget to put a sock in the chute.

Eventually, you run out of clean socks.

Replace socks with RAM. Your mom is the Operating System.

Anonymous 0 Comments

You’re an elementary school teacher with a bunch of scissors, but only enough for half the class. Students can come up to borrow them when they need. Most of the time the kids give the scissors back when they’re done, but sometimes they forget. Some kids will even forget to give theirs back and come ask for a second (or third) pair. If this keeps happening, eventually you don’t have any scissors to give out when asked. You will however always get them all back at the end of the day when the kids go home, but by that point a bunch of the kids might have failed to do their work for lack of scissors.

So, the teacher is the operating system and the scissors are the RAM (memory). Programs ask for RAM, the OS gives it out, but it’s not always returned when the program is done with it. If this keeps happening, eventually the computer/OS has no more RAM to give out to programs that ask for it and everything grinds to a halt. If you close the bad program the OS will know that RAM is obviously free again, but as long as it’s running it will constantly be reserving RAM and then forgetting it’s reserved it (and likely asking for more). It’s supposed to be a loop but when it breaks, that’s the “leak”.

Anonymous 0 Comments

I have that in my freezer.
Every time I eat one bread I leave the crust parts in the bag in the freezer.
So that just occupies a bit of space there. Well the next bread still fits, and also the next, but after a while, It gets harder and harder to find place for a full bread, because the space is taken by the left overs from the old breads.
It leaks freezer space, so to say.

This freezer space is my computer memory and the new breads are programs being started and closed every time.

Until someone gets rid of the old bags of leftovers, my computer has more and more problems to start in new programs.

Anonymous 0 Comments

When you write a program, you can *allocate* memory, that is, you can ask the OS for a block of memory you can use to keep track of stuff. For example, if you’re writing a computer game, you might ask for a small block of memory for each enemy character, to keep track of its location and health.

Once you’ve done this, you can later *free* the memory you were allocated. This means you tell the OS “I’m done using this memory”.

If you keep allocating memory, but you forget to free the memory, eventually the computer will run out of memory, causing your program to slow down or crash.

For example, it’s possible you might write your game code to stop displaying an enemy whose health is zero (and stop checking that enemy for collisions with players, projectiles or other enemies).

If you programmed your game like this, when you test play your game, you’d see enemies “die” as expected when they run out of health. But even though the enemy no longer affects gameplay, the block of memory that contains the enemy’s last known position and its health (now 0) is still there. If you leave the game running for hours or days, it’s possible that new enemies will continually allocate new blocks of memory but old dead enemies will never free their memory.

Your program “looks” correct when you test it with a normal playthrough, but there’s a hidden problem when you leave it running for a long time in a certain part of the game. That problem will make the game use more and more memory and eventually crash.