Skip to content

[Linux] Use "free" command to Release Memory

Recently, I want to experiment the execution speed of Linux program, in order to avoid being affected by the memory loading when measuring the speed, some commands are queried to clear the memory.

Now that I have tested and learned some knowledge about memory in Linux system, I recorded them all.


free Command

we can use the following command to display the memory value in Linux:

free -m

Output:

Mem is our memory. You can see that my laptop has about 12GB, and it uses about 5GB.

And Swap refers to a space located on a "hard disk". When our physical memory is occupied, but the system needs more memory resources, the system will move part of the static data to Swap.

However, Swap is certainly not as fast as real memory.


Release Memory

In fact, in Linux, we generally don't need to clean up our memory ourselves (because Linux manages it very well). If we really want to manually release the memory capacity, we can enter the "control code" into the "/proc/sys/vm/drop_caches" file to start the Linux system to clean up the memory.

As for the types of control codes, there are three types:

  1. Clear Page Cache
  2. Clear Dentries and Inodes
  3. Clear all the above categories

Page Cache is the Cache of general file data. Dentries and Inodes are like the numbers of folders and files.

According to my understanding, be careful to clear Inodes when cleaning memory. After all, the "recovery tools" that have accidentally deleted files on Linux all rely on retrieving files through Inodes.

The topic is a bit far off. All in all, to enter the control code, we need root permission.

sudo -i
sync; echo 3 > /proc/sys/vm/drop_caches

In this way, we cleared Page Cache, Denries, and Inodes in one go.


References


Read More

Tags:

Leave a Reply