A computer remembers things in more than one way. The two big ones are RAM and storage, a desk and a cabinet. Today you'll fill them up (safely!) and watch the numbers move. 🧮
- RAM (memory) is the desk: small and fast. Things you're using right now sit on it. When the power goes off, the desk is cleared.
- Storage (an SSD, hard drive or SD card) is the cabinet: big and slower. It keeps files even with the power off.
🧪 Lab 1: read the desk meter (RAM)
free -h
Check the numbers again:
total used free shared buff/cache available
Mem: 3.8Gi 612Mi 2.4Gi 22Mi 860Mi 3.2Gi
Swap: 1.0Gi 0B 1.0Gi
- used is memory programs are using right now.
- buff/cache is memory Linux uses to remember recently read files, so it can open them faster next time. (It gives this back instantly if a program needs it!)
- available is the number that really matters: how much a new program could use.
- Swap is extra memory on the cabinet used when the desk is full.
Want to see what the OS keeps track of? Peek at its own counters:
head -5 /proc/meminfo
/proc is a fake folder: the OS fills it with live information, and it isn't stored on any drive!
🧪 Lab 2: read the cabinet meter (storage)
df -h /
This shows the main drive: Size, Used, Avail and Use%. Remember the Avail number.
du -sh ~/MagiLab
du means disk usage. This tells you how much space your lab folder takes. (It's tiny right now!)
🧪 Lab 3: fill the cabinet on purpose
Let's make a 50 MB file full of zeros, inside the lab only. First, look at the cabinet in megabytes (-m), because -h rounds small changes away on big drives:
cd ~/MagiLab
df -m /
Remember the Used and Available numbers. Now make the file:
head -c 50000000 /dev/zero > big.bin
ls -lh big.bin
du -sh ~/MagiLab
df -m /
/dev/zerois a device that gives endless zeros.head -c 50000000takes the first 50 million of them.ls -lhshows the file size in a friendly way, anddushows your lab folder grew.- In the second
df -m /, Used went up by about 48 and Available went down by about 48. You used cabinet space!
Now clean up. Always delete your experiment:
rm big.bin
df -m /
The numbers come back. 🎉
⚠️
rmhas no trash can. On the command line, a deleted file is gone for good. That's why we only delete files we made in~/MagiLab.
🧪 Lab 4: a disk made of RAM
df -h /dev/shm
/dev/shm is a RAM disk: a folder that lives in memory, not on the cabinet. Files placed there are super fast, but they vanish when the computer is switched off.
echo "I live in RAM" > /dev/shm/magi.txt
cat /dev/shm/magi.txt
free -h
rm /dev/shm/magi.txt
🧪 Lab 5: what if the desk is full?
swapon --show
free -h | grep -i swap
If you have swap, the OS can borrow cabinet space as extra desk. It helps, but storage is slower than RAM, so the computer feels sluggish. (If both show nothing, you have no swap, which is fine.)
🧠 Memory protection: the OS gives each process its own area of RAM and doesn't let it touch the others. It's like each student having a locked desk.
How big is big?
| Unit | Roughly |
|---|---|
| KB | A short text message |
| MB | A song or a few photos |
| GB | Around 1,000 MB, like a movie |
| TB | Around 1,000 GB, a whole library |
