What Happens When You Request 8 GB Memory on a 4 GB Machine? – Virtual Memory, Swap and OOM Explained
The article explains how requesting memory larger than physical RAM interacts with virtual memory, page faults, swap mechanisms, and OOM handling on 32‑bit and 64‑bit operating systems, illustrated with C code experiments and Linux/macOS observations.
When an application calls malloc , it actually reserves virtual memory, not physical RAM. If the virtual pages are later accessed, the CPU triggers a page‑fault interrupt, and the kernel’s page‑fault handler tries to map those pages to free physical memory.
On a 32‑bit OS the virtual address space is limited (typically 3 GB for user space). Therefore a request for 8 GB will fail during the allocation phase because the address space cannot accommodate it.
On a 64‑bit OS the virtual address space is huge (up to 128 TB). An 8 GB request succeeds because it only reserves virtual pages; physical memory is allocated only when the pages are touched. Whether the program can actually use the memory depends on the presence of swap.
If swap is disabled and the program accesses more memory than the available physical RAM, the kernel cannot reclaim enough pages and the OOM killer terminates the process. If swap is enabled, the kernel can move inactive pages to disk, allowing the program to continue running even when the physical RAM is exhausted, though at the cost of heavy disk I/O.
Two experiments demonstrate this behavior:
Experiment 1 (no swap): a 64‑bit server with 2 GB RAM allocated four 1 GB blocks via malloc . Accessing the second block caused the OOM killer to kill the process.
Experiment 2 (swap enabled): a macOS laptop with 8 GB RAM allocated 32 GB of virtual memory and continuously accessed it. The process ran successfully, using swap (≈2.3 GB) and generating noticeable disk I/O.
The article also clarifies that swap works by writing rarely‑used anonymous pages (heap, stack) or file‑backed pages to a dedicated disk area (swap partition or swap file) and reading them back when needed. Swap extends usable memory but is limited by disk speed and size; excessive swapping still leads to OOM if the system cannot satisfy memory demands.
In summary:
32‑bit OS: maximum user virtual memory ≈3 GB → 8 GB request fails.
64‑bit OS: virtual memory up to 128 TB → 8 GB request succeeds. No swap → OOM killer terminates the process when physical memory is exhausted. Swap enabled → process can use the requested memory, but heavy swapping degrades performance.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define MEM_SIZE 1024*1024*1024
int main() {
char* addr[4];
for (int i = 0; i < 4; ++i) {
addr[i] = (char*)malloc(MEM_SIZE);
if (!addr[i]) {
printf("malloc failed, error: %s\n", strerror(errno));
return -1;
}
printf("malloc succeeded, address: 0x%x\n", addr[i]);
}
for (int i = 0; i < 4; ++i) {
printf("Accessing block %d\n", i+1);
memset(addr[i], 0, MEM_SIZE);
}
getchar();
return 0;
} #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MEM_SIZE 32*1024*1024*1024
int main() {
char* addr = (char*)malloc((long)MEM_SIZE);
printf("Allocated 32GB virtual memory\n");
while (1) {
printf("Accessing 32GB virtual memory...\n");
memset(addr, 0, (long)MEM_SIZE);
}
return 0;
}IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.