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
topon Terminal 1, and write down how muchfreememory you have (keep it running for the rest of this module)(term 1) root:~# top
- Start the memory hog
hog.shon Terminal 2, let it run until it gets killed (if it hangs- useCtrl+c)(term 2) root:~# bash 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
BuffersandCachedvalues inman 5 proc(undermeminfo)- Run the memory hog on Terminal 2
scripts/memory/hog.sh
(term 2) root:~# bash linux-metrics/scripts/memory/hog.sh
- Write down the
buffersize from Terminal 1 - Now run the buffer balloon
buffer.shon Terminal 2
(term 2) root:~# bash linux-metrics/scripts/memory/buffer.sh
- Check the
buffersize again - Read the script, and see if you can make sense of the results
- Repeat all 5 steps above with the
cached Memvalue - Repeat all steps for
cache.sh
(term 2) root:~# bash linux-metrics/scripts/memory/cache.sh
- Run the memory hog on Terminal 2
- Let's see how
cached Memaffects 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.pyscript 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 (term 2) root:~# time ls trash
- Run the
dentry2.pyscript 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.pyanddentry2.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?
- Run the following stress test, what do you see?
(term 2) root:~# bash stress -m 18 --vm-bytes 100M -t 600s
- Most tools use
/proc/meminfoto fetch memory usage information.- A simple example is the
freeutility - What does the 2nd line of
freetell 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
syncwhilesaris running)
- Here you can also see how many dirty pages you have (try running