Forgetting To Free Memory
This page is a generated reference surface for selective reading. It exists to keep the learner apps guide-first while still preserving source access.
Learning objectives
- Explain the main ideas and vocabulary in Forgetting To Free Memory.
- Work through the source examples for Forgetting To Free Memory without depending on raw chunk order.
- Use Forgetting To Free Memory as selective reference when learner modules point back to Ostep.
Prerequisites
- None curated yet.
Module targets
module-02-memory-management-virtual-memory
AI companion modes
- Explain simply
- Socratic tutor
- Quiz me
- Challenge my understanding
- Diagnose my confusion
- Generate extra practice
- Revision mode
- Connect forward / backward
Source-of-truth note
This unit is anchored to Ostep and the source chapter "Forgetting To Free Memory". Use external resources only to clarify, extend, or modernize details without replacing the chapter's conceptual spine.
External enrichment
No chapter-specific enrichment resources are curated yet. Add them in the unit manifest when a source clearly improves learning.
Source provenance
- Primary source:
Ostep - Source chapter: Forgetting To Free Memory
- Raw source file:
062-forgetting-to-free-memory.md
Merged source
Forgetting To Free Memory
Forgetting To Free Memory
Another common error is known as amemory leak, and it occurs when you forget to free memory. In long-running applications or systems (such as the OS itself), this is a huge problem, as slowly leaking memory eventually leads one to run out of memory, at which point a restart is required.
Thus, in general, when you are done with a chunk of memory, you should make sure to free it. Note that using a garbage-collected language doesn't help here: if you still have a reference to some chunk of memory, no garbage collector will ever free it, and thus memory leaks remain a problem even in more modern languages.
In some cases, it may seem like not callingfree()is reasonable. For
example, your program is short-lived, and will soon exit; in this case, when the process dies, the OS will clean up all of its allocated pages and thus no memory leak will take place per se. While this certainly "works" (see the aside on page 7), it is probably a bad habit to develop, so be wary of choosing such a strategy. In the long run, one of your goals as a programmer is to develop good habits; one of those habits is understanding how you are managing memory, and (in languages like C), freeing the blocks you have allocated. Even if you can get away with not doing so, it is probably good to get in the habit of freeing each and every byte you explicitly allocate.
Freeing Memory Before You Are Done With It
Sometimes a program will free memory before it is finished using it; such a mistake is called adangling pointer, and it, as you can guess, is also a bad thing. The subsequent use can crash the program, or overwrite valid
memory (e.g., you called free(), but then called malloc()again to
allocate something else, which then recycles the errantly-freed memory).
Freeing Memory Repeatedly
Programs also sometimes free memory more than once; this is known as thedouble free. The result of doing so is undefined. As you can imagine, the memory-allocation library might get confused and do all sorts of weird things; crashes are a common outcome.
Callingfree()Incorrectly
One last problem we discuss is the call offree()incorrectly. After all,
free()expects you only to pass to it one of the pointers you received
frommalloc()earlier. When you pass in some other value, bad things can (and do) happen. Thus, such invalid frees are dangerous and of course should also be avoided.