Memory
一句话解释
Memory 是 temporary storage 哪里 program keeps 它的数据 and instructions 虽然运行.
用大白话说
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.
示例
// 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 stackPrimitives (数字, 字符串) 是存储 on stack. 对象 and 数组是 存储 on heap, 使用引用 on stack pointing 到它们.
相关术语
相关资源
相关课程
- Memory and 引用
学习关于 memory and 引用.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。