linux的缓存内存 cache memory,关于linux的Cache Memory

Linux与Windows不同,存在Cache Memory,有些时候你会发现没有什么程序在运行,但是使用top或free命令看到可用内存会很少。

什么是Cache Memory?

Cache Memory又叫缓存内存,当你读写文件的时候,Linux内核为了提高读写效率与速度,会将文件在内存中进行缓存,这部分内存就是缓存内存。即使你的程序运行结束后,Cache Memory也不会自动释放。这就会导致Linux系统中程序频繁读写文件后,你会发现可用物理内存会很少。

Linux的内存管理机制,一般不需要手动释放已经使用的Cache Memory。这些Cache Memory可以增加文件的读写速度,其实Cache Memory在你需要使用内存的时候会自动释放,所以你不必担心没有内存可用。如果你希望手动去释放Cache Memory的话也是有办法的。

使用free命令查看内存使用情况

Default

[root@phpally ~]# free

total used free shared buffers cached

Mem: 8058600 1844136 6214464 0 49492 1117336

-/+ buffers/cache: 677308 7381292

Swap: 490488 0 490488

1

2

3

4

5

[root@phpally~]# free

totalusedfreesharedbufferscached

Mem:8058600184413662144640494921117336

-/+buffers/cache:6773087381292

Swap:4904880490488

total — 总物理内存

used — 已使用内存,一般情况这个值会比较大,因为这个值包括了cache+应用程序使用的内存

free — 完全未被使用的内存

shared — 应用程序共享内存

buffers — 缓存,主要用于目录方面,inode值等(ls大目录可看到这个值增加)

cached — 缓存,用于已打开的文件

-buffers/cache — 应用程序使用的内存大小,used减去缓存值

+buffers/cache — 所有可供应用程序使用的内存大小,free加上缓存值

其中第一行用全局角度描述系统使用的内存状况,第二行描述应用程序的内存使用情况,第三行表示swap的使用情况。

计算方法

total = used + free

used = buffers + cached (maybe add shared also)

-buffers/cache = used – buffers – cached

+buffers/cache = free + buffers + cached

Cache Memory的手动释放

Default

[root@phpally ~]# sync

To free pagecache:

[root@phpally ~]# echo 1> /proc/sys/vm/drop_caches

To free dentries and inodes:

[root@phpally ~]# echo 2> /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:

[root@phpally ~]# echo 3> /proc/sys/vm/drop_caches

1

2

3

4

5

6

7

8

9

10

[root@phpally~]# sync

Tofreepagecache:

[root@phpally~]# echo 1> /proc/sys/vm/drop_caches

Tofreedentriesandinodes:

[root@phpally~]# echo 2> /proc/sys/vm/drop_caches

Tofreepagecache,dentriesandinodes:

[root@phpally~]# echo 3> /proc/sys/vm/drop_caches

释放前最好用sync命令同步一下,防止丢失数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值