Chapter 31: Bash Memory Usage (free)
What does free actually do? (super simple first)
free = shows you memory usage statistics (RAM + swap) in a very quick, readable way.
It tells you:
- How much total RAM your computer has
- How much is used, free, shared, in buffers/cache, and available
- How much swap (virtual memory on disk) is used
This is one of the first commands almost every Linux user runs when the system feels slow, when a program crashes with “out of memory”, or when they want to check if they need more RAM.
1. Basic usage (try right now!)
Just type:
|
0 1 2 3 4 5 6 |
free |
You will see something like this (example from a typical 16 GB laptop in 2026):
|
0 1 2 3 4 5 6 7 8 |
total used free shared buff/cache available Mem: 16252928 8423672 1245696 589432 6583560 7123456 Swap: 2097148 0 2097148 |
Numbers are in KiB (kibibytes = 1024 bytes) by default → hard to read.
2. The MOST important way – human readable (always use this!)
|
0 1 2 3 4 5 6 7 8 |
free -h # or free --human |
Now it shows in GB / MB – much easier:
|
0 1 2 3 4 5 6 7 8 |
total used free shared buff/cache available Mem: 15Gi 8.0Gi 1.2Gi 570Mi 6.3Gi 6.8Gi Swap: 2.0Gi 0B 2.0Gi |
3. Understand every column (very important – many people get confused here!)
| Column | What it really means | Example value | When to worry |
|---|---|---|---|
| total | Total physical RAM installed | 15Gi | – |
| used | RAM actively used by processes (but not including cache) | 8.0Gi | High is normal if available is good |
| free | Completely unused RAM (almost never high on healthy system) | 1.2Gi | Low free is normal & good |
| shared | Memory used by tmpfs (RAM disks) + memory shared between processes | 570Mi | Usually small |
| buff/cache | RAM used for disk cache + buffers (very good – Linux uses free RAM for speed) | 6.3Gi | High is excellent |
| available | The most important number! → How much RAM programs can still use right now | 6.8Gi | <1–2 GiB → may start swapping |
| Swap total/used/free | Swap space on disk (virtual memory) | 2.0Gi / 0B / 2.0Gi | Swap used > 0 → performance drop |
Golden rule everyone should remember (2026 style):
- free column being low → completely normal and healthy
- Linux loves using “free” RAM for buff/cache to make disk reads super fast
- The only number you should really care about is available → if available drops below ~10–15% of total RAM for long time → system will start using swap → becomes slow
4. Very useful options (you’ll use these often)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# Most common combo – human + total line free -h --total total used free shared buff/cache available Mem: 15Gi 8.1Gi 1.1Gi 580Mi 6.2Gi 6.7Gi Swap: 2.0Gi 0B 2.0Gi Total: 17Gi 8.1Gi 1.1Gi # Show in megabytes (sometimes useful in scripts) free -m # Update every 2 seconds (like watch) watch -n 2 free -h # Only show memory line (clean output for scripts) free -h | grep Mem # Show detailed breakdown (very educational) free -w -h # -w = wide – shows old "cached" column separately (for old habits) |
5. Real-life examples you will use daily
- Laptop feeling slow after opening 20 Chrome tabs?
Bash01234567free -h# If available < 2Gi → close tabs or apps
- Checking before running heavy ML training (PyTorch / TensorFlow)
Bash01234567free -h# Need at least 8–10 Gi available for 16 Gi machine
- Server saying “out of memory” errors?
Bash01234567watch -n 3 free -h# Watch available drop → find culprit with htop / ps aux --sort=-%mem
- Is swap being used? (bad for performance)
Bash01234567free -h | grep Swap# If used > 100–200 MiB for long time → add more RAM or tune swappiness
- Quick check in script (example: alert if available < 2GB)
Bash0123456free -m | awk '/Mem:/ {if ($7 < 2048) print "Low memory!"; else print "OK"}'
6. Quick cheat-sheet table
| Goal | Command example | What to look at |
|---|---|---|
| Quick readable view | free -h | available column |
| With total line | free -h –total | Total RAM + used |
| Live monitoring | watch -n 2 free -h | Watch available drop |
| Only memory line | free -h |
grep Mem |
| In megabytes | free -m | Scripts / old eyes |
| Wide view (shows cached separately) | free -wh | Compare old vs new style |
| Check if swap is used | free -h |
grep Swap |
7. Pro tips & common misunderstandings (very important!)
- Myth: “My free RAM is only 1 GB – bad!” → No! Low free is good if available is high – Linux is using it for cache.
- Most important number = available (It already subtracts what kernel thinks you can’t safely use)
- Swap used a lot → system will feel very slow (especially on HDD) → Solution: close heavy apps, add RAM, or lower vm.swappiness (default 60)
- Modern systems (16–32 GB) often show 0B swap used – that’s ideal
Now open your terminal and try these 3 right now:
|
0 1 2 3 4 5 6 7 8 |
free -h free -h --total watch -n 3 free -h # press Ctrl+C to stop |
- How much available RAM do you have right now? (e.g. 6.8Gi)
- Is your swap used 0B or some value?
Or ask:
- “What to do if available is always below 2 GB?”
- “free vs htop vs top – which shows memory best?”
- “How to reduce swap usage?”
We’ll go deeper together! 😄
