(Turn) Detailed explanation of Cache Memory in Linux

A friend asked me the day before yesterday, why is my Linux system not running many programs and showing so little available memory?

In fact , the memory management of Linux and Win is different. It will try to cache memory to improve read and write performance, which is usually called Cache  Memory . Sometimes you will find that there are no programs running, but use the top or free command to see that there are very few free memory items available. At this time, check the /proc/meminfo file of the system and you will find an item Cached Memory:

Enter cat /proc/meminfo to view:

MemTotal: 16425996 kB
MemFree: 5698808 kB
Buffers: 380904 kB
Cached: 9389356 kB
SwapCached: 212 kB
Active: 6569200 kB
Inactive: 3725364 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 16425996 kB
LowFree: 5698808 kB
SwapTotal: 8273464 kB
SwapFree: 8273252 kB
Dirty: 980 kB
Writeback: 0 kB
AnonPages: 524108 kB
Mapped: 24568 kB
Slab: 381776 kB
PageTables: 7496 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
CommitLimit: 16486460 kB
Committed_AS: 2143856 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 267656 kB
VmallocChunk: 34359469303 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
Hugepagesize: 2048 kB

Description of various memory indicators in the free command:


total used free shared buffers cached
Mem: 16425996 10727220 5698776 0 380904 9389832
-/+ buffers/cache: 956484 15469512
Swap: 8273464 212 8273252

The first line describes the memory status used by the system from a global perspective:
total——total physical memory
used——used memory, in general, this value will be relatively large, because this value includes cache + memory used by the application program
free——completely Unused memory
shared - application shared memory
buffers - cache, mainly used for directories, inode values, etc. (ls large directory can see this value increase)
cached - cache, used for open files
Summary:
total=used+free
used=buffers+cached (maybe add shared also)

The second line describes the memory usage of the application:
the former value represents -buffers/cache - the amount of memory used by the application, and the value
after used minus the cache value represents +buffers/cache - all memory available to the application Size, free plus cache value
summary: 
-buffers/cache=used-buffers-cached
+buffers/cache=free+buffers+cached

The third line indicates the use of swap: used -
used
free - unused

What is Cache Memory:

When you read and write files, the Linux kernel caches files in memory in order to improve read and write performance and speed. This part of memory is Cache Memory. Even after your program finishes running, the Cache Memory will not be released automatically. This will cause you to find that the available physical memory will be very small after the program frequently reads and writes files in the Linux system.

In fact, the cache memory (Cache Memory) will be automatically released when you need to use the memory, so you don't have to worry about no memory available. If you want to release Cache Memory manually, there is also a way.

How to release Cache Memory:

Cache Memory can be released with the following command: Note that it is best to sync before releasing to prevent data loss.

To free pagecache:
echo 1 > /proc/sys/vm/drop_caches
To free dentries and inodes:
echo 2 > /proc/sys/vm/drop_caches
To free pagecache, dentries and inodes:
echo 3 > /proc/sys/vm/drop_caches

Summary: Personal experience thinks that there is no need to manually release, this memory management method is also one of the advantages of win! Because of Linux's kernel memory management mechanism, under normal circumstances, it is not necessary to release the cache that has been used. These cached contents can improve the read and write speed of files and disks.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326178899&siteId=291194637