Week 5: Memory and Swap
task
(prac-2).unit
of the Virtual Address Space is a “page”.The code (binary) of the program, loaded to RAM.
That’s where global and static variables live, since they will live until the process dies.
Every time we call a function, it pushes it contents to the stack
. The variables initialized on this function also live inside his stack frame
Pointers can be scary, but let’s see them in action!
If we don’t know how large an array (or whatever data structure) needs to be, or if we need to store data across multiple stack frames, we can use the heap.
We can allocate memory in this region of the virtual address space and free it when it’s no longer needed.
Unlike the stack, the heap doesn’t have a fixed size limit (other than available system memory).
There are two syscalls brk/sbrk
and mmap
for allocating memory and unmmap
/ brk/sbrk
for deallocating.
You’ll probably never use the syscalls, and use the glibc implementation called malloc
and free
.
Linux will first try to free up memory by flushing file-related caches (meaning it writes modified file data from RAM back to disk).
But what happens if most of the memory was allocated using malloc? In that case, there’s no corresponding file on the filesystem to write this data to! At this point, the system has two options:
Kill the process: The Out-Of-Memory (OOM) killer from the kernel may terminate one or more processes to free up memory.
Use swap space: The kernel can move memory pages to a swap partition or swap file. When a process later needs those pages, a page fault occurs, and the kernel retrieves the data from disk back into RAM.
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
nvme0n1 259:0 0 476,9G 0 disk
├─nvme0n1p1 259:1 0 512M 0 part /boot
├─nvme0n1p2 259:2 0 4G 0 part [SWAP]
└─nvme0n1p3 259:3 0 472,4G 0 part
└─crypted 254:0 0 472,4G 0 crypt
└─root_vg-root 254:1 0 472,4G 0 lvm /
You can create a swapfile with mkswap
and activate it with swapon
.
Create (do everything as root):
Enable (as root):
Disable it:
We have time?
If you want to enable the swapfile every time your machine boots, you can add a systemd service that enables it
free
command.malloc
glibc function.memset
function or a loop for assigning values) 1free
that swap is being used.Linux Kernel Developer talks about swap: https://chrisdown.name/2018/01/02/in-defence-of-swap.html
Arch Linux Wiki: https://wiki.archlinux.org/title/Swap
Linux Kernel docs talk about swap and hibernation: https://docs.kernel.org/power/swsusp-and-swap-files.html
Swapon syscall definition: https://elixir.bootlin.com/linux/v6.16.4/source/mm/swapfile.c#L3260
If you really want to understand a little bit more what happens under the hood, you can do the following exercices. Be aware that you should read the “Really Recommended References” first, and then try to do this exercices.
We have time?
Observe how the output of the free
command changes each time you run prac-3.1
with different swappiness values (e.g., 0 and 100). Then, create some plots to visualize the differences!
Ready to have some fun? Check out the third AMSA activity here!
AMSA 2025-2026 - 🏠 Home