Skip to content

Memory

In one line

Memory is the temporary storage where a program keeps its data and instructions while running.

In simple words

Memory (RAM) is where your program lives while it runs. When you create a variable, the computer sets aside a piece of memory to hold its value. When the function ends, that memory can be reused.

There are two main regions of memory: the stack and the heap. The stack stores local variables and function call information — it is fast and automatically managed. The heap stores larger, longer-lived data like objects and arrays — it is slower and must be managed (manually in C/C++, automatically via garbage collection in JavaScript/Python/Java).

Understanding memory helps you write efficient code and avoid problems like memory leaks (memory that is allocated but never freed) and stack overflows (too many nested function calls). In most modern languages, memory management is automatic, but the concepts still matter.

Example

javascript
// Stack: primitive values (fast, short-lived)
let x = 42;
let y = "hello";

// Heap: objects and arrays (longer-lived)
let obj = { name: "Ada", age: 36 };
let arr = [1, 2, 3, 4, 5];

// obj and arr are references (pointers) to heap memory
// x and y are values stored directly on the stack

Primitives (numbers, strings) are stored on the stack. Objects and arrays are stored on the heap, with references on the stack pointing to them.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary