IntermediateTopic 40 of 104Software & Computing
Memory management
Stack versus heap, garbage collection and manual allocation.
In depth
Memory is divided into stack (automatic, fast, scoped), heap (dynamic, manual or garbage-collected) and static areas. Leaks, dangling pointers and fragmentation are risks in manual management; garbage collectors (mark-sweep, generational) or ownership (Rust) provide safety. Allocation patterns affect performance.
Key points
- Reference counting
- Pause times
- Memory leaks
ExampleA forgotten free() in C leaks memory each loop iteration.
Memory management decides how programs use RAM, and mistakes lead to leaks, crashes and slow performance.
What is Memory management?
Stack versus heap, garbage collection and manual allocation.
Why is it important to understand memory management?
Memory management decides how programs use RAM, and mistakes lead to leaks, crashes and slow performance.
What is the difference between stack and heap?
The stack holds local variables and call frames with automatic lifetime, while the heap holds dynamically allocated data that must be freed or collected.
What is garbage collection?
A runtime process that finds and frees memory no longer reachable from the program, which removes manual freeing but can add pauses.
How is Memory management used in practice?
Typical applications include understanding why a long-running service slowly uses more memory, choosing between stack and heap allocation for a large array and reasoning about garbage collection pauses in a real-time application.
What should I learn before Memory management?
Study "Exceptions and error handling" first (topic 39 of 104 in Software & Computing). It covers the groundwork this topic builds on.
What are the key points of Memory management?
Reference counting. Pause times. Memory leaks.
Can you give an example of Memory management?
A forgotten free() in C leaks memory each loop iteration.
What comes after Memory management?
The next topic is "Pointers and references", which builds directly on this one.
How difficult is Memory management?
It is classed as Intermediate in Software & Computing (topic 40 of 104). It is an intermediate topic. Work through the beginner topics in Software & Computing first.