Understanding memory information on Linux systems
Linux comes with many commands to check memory usage. The “free” command usually displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel. The “top” command provides a dynamic real-time view of a running system. The top command can display system summary information as well as a list of the process currently being managed by the Linux kernel.
We will use free and top to understand the memory utilization of our operating system.
the free command comes with the set of options you can choose to modify the results, like with option “-m” result will display memory in MegaByte. you can read more about options on the manpage.
Let’s see the below example and understand:
In this example, total memory is 11901 MB, 8957 MB is used and 2943 MB free. But that does not just mean that application now can only request for 2943 MB free memory, If you look at the usage figures you can see that 5941 MB memory use is for buffers and cache. So, if applications request memory, then Linux OS will free up the buffers and cache to yield memory for the new application requests. So, a question arises, why to cache and buffer at the first place?
Linux by default tries to use RAM in order to speed up disk operations by making use of available memory for creating buffers (file system metadata) and cache (pages with actual contents of files or block devices), helping the system to run faster because disk information is already in memory which saves I/O operations.
Now, let us understand the memory utilization by processes, we will use “top” command to get the required information. “top” comes with a lot of information by default, but to get all the information we needed we will add two more columns CODE and DATA. please read more about modifying default behavior of top command on the manpage.
In the above example let’s discuss first process “Xorg”. The important tables for memory to look are %MEM, VIRT, RES, SHR, CODE, DATA.
- %MEM is directly related to RES, it’s the percentage use of total physical memory by the process.
- VIRT is the total memory that this process has access to shared memory, mapped pages, swapped out pages, etc.
- RES is the total physical memory used shared or private that the process has access to.
- SHR is the total physical shared memory that the process has access to.
- DATA is the total private memory mapped to process physical or not.
- CODE also known as “text resident set” is total physical memory used to load application code.
So, to sum up, RES is most close to the memory used by the process in memory, excluding what’s swapped out. but keep in mind that includes the SHR (shared physical memory) which mean it could have been used by some other process as well.
This is just a brief introduction about memory utilization in Linux. I will soon be making a screencast explaining all of these concepts in depth.