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 muchfree
memory 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
buffer
andcached Mem
values inman 5 proc
(undermeminfo
)- Run the memory hog
scripts/memory/hog.sh
- Write down the
buffer
size - Now run the buffer balloon
sudo scripts/memory/buffer.sh
- Check the
buffer
size again - Read the script, and see if you can make sense of the results...
- Repeat the 5 steps above with the
cached Mem
value andsudo scripts/memory/cache.sh
- Run the memory hog
- Let's see how
cached Mem
affects 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.py
script 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.py
script and try dropping the caches. Does it make a difference? what's the difference betweendentry.py
anddentry2.py
?
-
Why wasn't our memory hog able to grab all the
cached
memory? -
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/meminfo
to fetch memory usage information.- A simple example is the
free
utility
- 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) - The
%commit
field is also interesting, especially if it's larger than 100...
- Here you can also see how many dirty pages you have (try running