In this video, I want to tell you a little bit more about analyzing memory statistics.
First, it helps if you know about the right terminology.
Cache is an area of memory that is used as temporary storage
for frequently accessed data for faster access.
You have cache at different levels.
From disk, you cache in RAM.
There's internet cache, for instance, as well, that can be used on a proxy,
which is at a completely different level.
Linux buffer memory is used for temporary storage in memory for data while it is moved.
Most commonly used are the write buffers.
So when you write a file, the file is committed to memory,
and then it sits in the buffers until it's time to flush the data to disk.
Anonymous memory is memory that is allocated by applications.
Swap is emulated RAM on disk, which can be used for anonymous memory.
Virtual memory is an address space that is determined by the CPU architecture.
It has nothing to do with swap.
The thing is that every Linux application wants to allocate a certain amount of memory.
That's a reservation.
That doesn't mean that it is used,
but it means that it is blocked for that specific application.
And from the virtual memory, a resident memory is obtained,
and that is memory that is actually being used.
Active memory is memory that has recently been used,
and inactive memory is memory that has not recently been used.
This is pretty important for the Linux kernel to determine
which memory pages are a candidate for moving to swap,
or if the memory pages are in cache.
The Linux kernel uses this to determine which memory pages can be dropped
if more memory is needed.
As I just mentioned, inactive file memory is eligible to be dropped
in case of memory shortage,
and inactive anonymous memory is eligible to be moved to swap
in case of memory shortage.
Now, if memory shortage is bad,
then active anonymous memory may be moved to swap as well,
and that is also bad because your system will be very busy moving data
in and out of swap, and that won't make it any faster.
To monitor memory, 3-M is a good start for a generic overview.
VMstat gives more detailed memory usage information,
and for the ultimate detail, you want to check out cat-proc meminfo.
That is where the kernel stores information about memory.
Let's check it out.
So here we have 3-M output, where we see the total amount of memory,
the amount of memory that is used, and the amount of memory that is free.
And, hey, in case you are thinking, well, between used and free and total,
there's a difference. What is in this difference?
Well, that is your buffer cache.
Total memory is buffer cache plus shared plus free plus used.
Now, the available parameter considers memory that is currently free,
but also inactive memory in cache,
which can be cleared immediately to make space for more memory.
This system also has 2 GB of swap,
but the 2 GB of swap is not used at all, and that is what we see here.
There's no need to use swap, and the Linux kernel doesn't favor
moving inactive anonymous memory to swap if it doesn't have to.
I mean, we have enough memory available here.
Now, VMstat is used with a couple of parameters.
I'm using VMstat 2.5, which is more than enough.
The 2 is for a 2-second interval,
and the 5 means that I want to do it 5 times.
And right here, I can see my overview of memory,
and I can see SISO, which is indicating the movement of memory.
Swap in, swap out.
So if I have swap that is actively being used,
I should see a high activity here.
Now, finally, if you want to know more about the state of your system,
check out procmeminfo.
In procmeminfo, you find a lot of parameters.
The parameters that are most interesting are right here,
active and inactive memory.
So active is what really needs to be used
and needs to be in your RAM, and inactive,
well, it could be doing something else as well.
And as you can see on this system,
we have about 2 gigabytes of inactive memory,
and that explains a lot about the current state
of what we see in 3-M.