Is free memory really free? What's the difference between cached memory and buffers? Why can't I dump all the caches? Let's get our hands dirty and find out...
- Fire up
top, and write down how muchfreememory you have (keep it running for the rest of this module) - Start the memory hog
scripts/memory/hog.sh, let it run until it gets killed (if it hangs- useCtrl+c) - Compare that to the number you wrote. Are they (almost) the same? If not, why?
- Read about the
bufferandcached Memvalues inman 5 proc(undermeminfo)- Run the memory hog
scripts/memory/hog.sh - Write down the
buffersize - Now run the buffer balloon
sudo scripts/memory/buffer.sh - Check the
buffersize again - Read the script, and see if you can make sense of the results...
- Repeat the 5 steps above with the
cached Memvalue andsudo scripts/memory/cache.sh
- Run the memory hog
- Let's see how
cached Memaffects application performance- As root, drop the cache using
echo 3 > /proc/sys/vm/drop_caches - Time a dummy Python application
time python -c 'print "Hello World"'(you can repeat these 2 steps multiple times) - Now re-run our dummy Python application, but this time without flushing the cached memory. Can you see the difference?
- As root, drop the cache using
- Run the
memory/dentry.pyscript and observe the memory usage usingfree. What is using the memory? How does it effect performance? What tools can show you kernel memory usage? - Run the
memory/dentry2.pyscript and try dropping the caches. Does it make a difference? what's the difference betweendentry.pyanddentry2.py?
-
Why wasn't our memory hog able to grab all the
cachedmemory? -
What will happen if we remove the following line from
scripts/memory/hog.sh(Try it!)? Why?tmp[0] = 0;
-
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?
- Most tools use
/proc/meminfoto fetch memory usage information.- A simple example is the
freeutility
- 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) - The
%commitfield is also interesting, especially if it's larger than 100...
- Here you can also see how many dirty pages you have (try running