Skip to the content ForoHackers
Log inJoin

Memory management: start here

Memory management
#875 · Ophuscado · · Report

Memory management is how a computer and its software hand out, track and take back memory. It spans hardware translation of addresses, the operating system's paging and the allocator inside a program. Most crashes, leaks and security holes in low-level code come from getting it wrong, so understanding it pays off in every language.

Key concepts

  • Virtual memory: each process has its own address space, translated to physical pages by the hardware and the kernel.
  • Paging: memory moves in fixed-size pages, with page tables, a TLB and page faults that load pages on demand.
  • Stack and heap: the stack holds call frames and grows automatically; the heap holds data that outlives a call.
  • Allocators: malloc and similar libraries carve the heap into blocks and must limit fragmentation.
  • Reclamation: manual freeing, reference counting, ownership rules or a garbage collector decide when memory returns.

Vocabulary

  • Page fault: an access to a page not currently mapped, which the kernel resolves or rejects.
  • Fragmentation: free memory split into pieces too small to use.
  • Leak: memory no longer needed but never released.
  • Use after free: touching memory after it has been given back.

What to study, in order

  1. The address space of a process: print where code, stack and heap live.
  2. Pointers and manual allocation in C, with a leak and a use-after-free that you cause and catch with a sanitizer.
  3. Write a simple allocator: a free list, then splitting and coalescing blocks.
  4. Page tables and the TLB, from a computer architecture text, drawn by hand.
  5. Watch a process's memory use grow and swap with the system's monitoring tools.

Related tags

#kernels #c #rust #computer-architecture #debugging

Log in or join to reply.