Virtual memory là abstraction layer giữa process và RAM vật lý: mỗi process có không gian địa chỉ ảo riêng (64-bit: 128TB), OS + MMU (Memory Management Unit) dịch virtual address sang physical address qua page table.
Lợi ích:
- Isolation: process A không thể đọc bộ nhớ process B dù cùng máy
- Overcommit: tổng virtual memory của tất cả process có thể vượt RAM thực (OS swap ít-dùng pages ra disk)
- Shared libraries: nhiều process share cùng physical pages của libc nhưng map vào virtual address space riêng
- Memory-mapped files (mmap): file map vào virtual memory, OS lazily load pages khi access
- Copy-on-Write (CoW) khi fork: parent và child share physical pages cho đến khi một bên write
Nhược điểm: page table overhead (~8MB per process với 4-level paging); TLB miss latency. Thực tế: docker stats show virtual memory rất lớn nhưng RSS (Resident Set Size) là RAM thực dùng; OOM Killer kill process dùng nhiều RAM nhất khi system hết memory.
Virtual memory is an abstraction layer between a process and physical RAM: each process has its own virtual address space (128TB on 64-bit systems), and the OS + MMU (Memory Management Unit) translates virtual addresses to physical addresses via page tables.
Benefits:
- Isolation — process A cannot read process B's memory even on the same machine
- Overcommit — the combined virtual memory of all processes can exceed physical RAM, with the OS swapping less-used pages to disk
- Shared libraries — multiple processes share the same physical pages of libc while each maps them into its own virtual address space
- Memory-mapped files (mmap) — files are mapped into virtual memory and the OS lazily loads pages on access
- Copy-on-Write (CoW) on fork — parent and child share physical pages until one of them writes. Drawbacks: page table overhead (~8MB per process with 4-level paging) and TLB miss latency. In practice:
docker statsshows very large virtual memory, but RSS (Resident Set Size) reflects actual RAM usage; the OOM Killer terminates the process consuming the most memory when the system runs out