Is free memory really free? What's the difference between cached memory and buffers? Let's get our hands dirty and find out...
You will need 3 open terminals for this task. DO NOT RUN ANY SCRIPTS ON YOUR LAPTOP!
- Fire up
top
on Terminal 1, and write down how muchfree
memory you have (keep it running for the rest of this module):(term 1) root:~# top
- Start the memory hog
hog.sh
on Terminal 2, let it run until it gets killed (if it hangs- useCtrl+c
):(term 2) root:~# /bin/sh linux-metrics/scripts/memory/hog.sh
- Go to Terminal 1 and compare that to the number you wrote. Are they (almost) the same? If not, why?
- Read about the
Buffers
andCached
values inman 5 proc
(undermeminfo
):- Run the memory hog on Terminal 2
scripts/memory/hog.sh
:
(term 2) root:~# /bin/sh linux-metrics/scripts/memory/hog.sh
- Write down the
buffer
size from Terminal 1. - Now run the buffer balloon
buffer.sh
on Terminal 2:
(term 2) root:~# /bin/sh linux-metrics/scripts/memory/buffer.sh
- Check the
buffer
size again. - Read the script, and see if you can make sense of the results.
- Repeat all 5 steps above with the
cached Mem
value. - Repeat all steps for
cache.sh
:
(term 2) root:~# /bin/sh linux-metrics/scripts/memory/cache.sh
- Run the memory hog on Terminal 2
- Let's see how
cached Mem
affects application performance:- Drop the cache:
(term 2) root:~# echo 3 > /proc/sys/vm/drop_caches
- Time a dummy Python application (you can repeat these 2 steps multiple times):
(term 2) root:~# time python -c 'print "Hello World"'
- Now re-run our dummy Python application, but this time without flushing the cached memory. Can you see the difference?
- Run the
dentry.py
script and observe the memory usage usingfree
. What is using the memory? How does it effect performance?(term 2) root:~# python linux-metrics/scripts/memory/dentry.py (term 2) root:~# echo 3 > /proc/sys/vm/drop_caches (term 2) root:~# time ls trash/ >/dev/null (term 2) root:~# time ls trash/ >/dev/null
- Run the
dentry2.py
script and try dropping the caches. Does it make a difference?(term 2) root:~# python linux-metrics/scripts/memory/dentry2.py
- What's the difference between
dentry.py
anddentry2.py
? - Assuming a server has some amount of free memory, can we assume it has enough memory to support it's current workload? If not, why?
- Why wasn't our memory hog able to grab all the
cached
memory? - Run the following stress test, what do you see?
(term 2) root:~# stress -m 18 --vm-bytes 100M -t 600s
- Most tools use
/proc/meminfo
to fetch memory usage information.- A simple example is the
free
utility. - What does the 2nd line of
free
tell us?
- A simple example is the
- To get usage information over some period, use
sar -r <delay> <count>
.- Here you can also see how many dirty pages you have (try running
sync
whilesar
is running).
- Here you can also see how many dirty pages you have (try running