Contents

Computer Science › Computer Architecture

RAM

Fast, temporary memory for running programs.

Also known as: memory, main memory, random access memory

RAM (random access memory) is the computer’s fast, temporary memory. Running programs and the data they’re working on live here, because RAM is far faster than disk. When the power goes off, RAM is emptied.

CPU cache (tiny, fastest)  →  RAM (large, fast)  →  SSD / HDD (huge, slower, persistent)

RAM vs storage

RAMDisk (SSD or HDD)
Speedvery fast (nanoseconds)much slower (microseconds to milliseconds)
Persistencelost at power-offkept
Sizegigabyteshundreds of gigabytes to terabytes
Cost per GBhigherlower

A file on disk is loaded into RAM to be used, and saving writes it back (storage devices).

What uses it

  • Your program’s variables and objects: the stack and heap.
  • Caches, buffers and loaded files.
  • The operating system itself.

What happens when it runs out

  • Swapping: the OS moves memory to disk, which is far slower, so everything crawls.
  • The OOM killer / out-of-memory errors: the system or runtime kills a process. A container hitting its memory limit gets killed, a very common production problem (requests and limits).
  • MemoryError / heap out of memory exceptions in your language.

Developer concerns

  • Memory leaks: memory that’s never released grows until something dies (memory leak).
  • Garbage collection reclaims unused objects automatically in many languages (garbage collection).
  • Loading everything at once. Reading a huge file or query result into a list can exhaust memory. Stream or batch it (generators, database cursors).
  • Estimate sizes before designing: a million objects of 1 KB is a gigabyte (back-of-the-envelope).
  • Monitor memory use, not just CPU.