Monitor Memory Usage With Free
free
is a command which can give us information on available RAM in Linux machine. This post will summerize the usage of free
command and how to interpret the output.
Command usage
free
Display RAM in human readable formats like in KB’s, MB’s, GB’s, TB’s1.
free -k
free -m
free -g
free --tera
Display memory usage every 2 seconds.
free -s 2
Buffer, Cache and RAM
In linux, Used RAM = cached RAM + buffered RAM + actural RAM used by the applications and OS2.
- Cached RAM: cache is a memory location to store frequently used data for faster access. It is similar to the concept of cache in CPU’s architecture.
- Buffered RAM: buffer is a temporary location to store data for a particular application and this data is not used by any other application. Think of it as a buffer when you try to send a large message over a small-bandwidth network.
Both cache
and buffer
are available
, which means they are used but can be purged without significant effects on the running applications.
A simple calculation can be done as the following:
$ free -m
total used free shared buffers cached
Mem: 128896 89187 39709 2 467 67476
-/+ buffers/cache: 21242 107653
Swap: 4095 0 4095
In row 1 of output, total mem = used mem + free mem. Linux recognized buffer and cache as used
.
In row 2 of output, used
indicate the memory actually being used by OS and applications. To verify it, we have row 1 : 39709(free
)+ 467 (buffers
) + 67476 (cached
) = 107652, which is almost equal to 107653(free
at row 2). It means that we have more than 107GB of available memory currently.
As a rule of thumb3, a healthy linux system should have: \
- free memory is close to 0 \
- used memory is close to total \
- available memory (or “free + buffers/cache”) has enough room (let’s say, 20%+ of total) \
- swap used does not change \
Disk Caching and swapping
Disk caching3 is done by borrowing unused memory to cache the disk. This makes the system much faster and more responsive. If the applications demand more memory, they just take back a RAM that the disk cache used. Disk cache can always be given back to applications immediately. Thus, it is a feature that kind of behaves like “free lunch” and cannot be disabled.
On the contrarary, swapping happens when you are lacking physical memory. Swap is a virtual memory created on HDD to increase RAM size virtually.
free command by default counts 1024 as power instead of 1000. To view with 1000 as base, use –si ↩︎
many content are learned from UNDERSTANDING FREE COMMAND IN LINUX/UNIX ↩︎