Skip to the content ForoHackers
Log inJoin

Memory management: start here

Memory management
#3381 · · Quote · 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

You are not logged in, so this goes up anonymously: no account, no name and no address is stored with it, and it may carry no link and no image. Log in or join to post under your name. Our web server's logs keep the address and time of every request for up to 14 days; to keep yours out of them, use Tor. To reach the staff in confidence, use the drop. Sign as a returning source (a key kept only in this browser)

Sending takes a few seconds here: your browser first works out a proof of work against spam.

Reply