How to view the free memory in the system with top and free commands under CentOS


Here’s how to use the top and free commands to view the free memory in the system

So what you see when you execute the top command

[[email protected] ~]# top

MEM: 8174492k total, 7124268k used, which does not mean that your application has used 7.1 memory. This 7.1g includes: application memory + buffer + cache memory. You need to use the free command to view it

Here is an example (in MB):

[[email protected] ~]# free -m

                    total       used       free     shared    buffers     cached

Mem:          7982       6811       1171          0        350       5114

-/+ buffers/cache:       1346       6636

Swap:        16935         11      16924

[[email protected] ~]# 

In this example, the application uses only 1346mb of memory and 6636mb of free memory

Some simple calculation methods: 

Physical used memory = actual used memory buffer cache

= 6811M – 350M – 5114M

Physical free memory = total physical memory – actual used memory + buffer + cache

Free memory available for application = total physical memory – actual used memory

Application used memory = actual used memory buffer cache

Original explanation: go to the Internet:

The basic principle of Linux is that no resources should be wasted. Therefore, the core will use as much RAM as possible to cache information from local and remote file systems. When the system does read and write operations, it will store the data related to the current running process in RAM as much as possible. The cache reported by the system is the sum of the two. The cache is not recycled at the end of the process( You may soon start another process that requires the same data), but recycle on demand – for example, when you start a process that requires a lot of memory, the Linux core will recycle the cache from the memory and allocate the obtained memory to the new process

Some areas, such as anonymous memory mapping (MMPs) and shared memory area, are reported as cache, but not directly released by the core. General cache does not map to the address space of the process, but only simple core mapping, and these special cache maps to all processes attached to them