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:
- Clear Page Cache
- Clear Dentries and Inodes
- 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
- https://www.tecmint.com/clear-ram-memory-cache-buffer-and-swap-space-on-linux/
- https://unix.stackexchange.com/questions/17936/setting-proc-sys-vm-drop-caches-to-clear-cache
Read More
- [Linux] Use "ssh" Command to log in the Remote Server and Execute Commands
- [Linux] Use "ln" Command to Create Soft Link and Hard Link Files
- [Linux] How to Determine whether a Disk is SSD
- [Linux] Use mv and rename command to rename a file
- [Linux] Use "ls" command to display different colors and orders according to different file extensions