Linux使用free查看内存参数

free -m 通常用来查看linux的内存情况。

ginux@ginux:/mnt/c/Users/wangw$ free -h
              total        used        free      shared  buff/cache   available
Mem:          7.6Gi       286Mi       7.2Gi        24Mi       131Mi       7.1Gi
Swap:         2.0Gi          0B       2.0Gi

其中,total为总内存,used是使用的,free未使用的,shared是共享内存的大小,buff/cache 是缓存和缓冲区的大小,available 是新进程可用内存的大小。

关于 free 和 available 的区别:

实际上这两信息都来自于 /proc/meminfo,free对应 MemFree,free是内核真正空闲的物理内存;关于 available 的计算,比较复杂,推荐一个视频看吧:

ginux@ginux:/mnt/c/Users/wangw$ cat /proc/meminfo
MemTotal:        7996708 kB
MemFree:         7537532 kB
MemAvailable:    7461396 kB
Buffers:           12240 kB
Cached:           104108 kB
SwapCached:            0 kB
Active:            43476 kB
Inactive:         100648 kB
Active(anon):       2468 kB
Inactive(anon):    52736 kB
Active(file):      41008 kB
Inactive(file):    47912 kB
...

Buffer 和 Cache 可能不太好区分。从字面上来说,Buffer 是缓冲区,而 Cache 是缓存,两者都是数据在内存中的临时存储。

从 free 的手册可以得知:

  • Buffer 是内核缓冲区用到的内存,对应 /proc/meminfo 中的 Buffers 值。
  • Cache 是内核页缓存和Slab用到的内存,对应 /proc/meminfo 中的 Cached 和 SReclaimable 之和。

为了进一步了解这些符号,先来看一下 /proc

/proc 文件系统

/proc 是内核提供的一种特殊文件系统,是用户跟内核交互的接口。比如,用户可以从/proc中查询内核的运行状态和配置选项。查询进程的运行状态、统计数据等等。也可以通过/proc来修改内核的配置。

proc 文件系统同时也是很多性能工具的数据来源,比如 free,就是通过读取 /proc/meminfo,得到内存的使用情况。

继续说回/proc/meminfo,既然 Buffers、Cached、SReclaimable 这几个指标不容易理解,那我们还得继续查看 proc 文件系统。

cmd 执行 man proc 可以得到文件系统的详细文档。在man中的操作和vim类似,输入/加搜索字符就可以搜索关键字,如/meminfo,然后小写n向下查找,大写N向上查找。

 /proc/meminfo
              This  file  reports  statistics  about memory usage on the system.  It is used by free(1) to report the
              amount of free and used memory (both physical and swap) on the system as well as the shared memory  and
              buffers  used  by the kernel.  Each line of the file consists of a parameter name, followed by a colon,
              the value of the parameter, and an option unit of measurement (e.g., "kB").  The list  below  describes
              the  parameter names and the format specifier required to read the field value.  Except as noted below,
              all of the fields have been present since at least Linux 2.6.0.  Some fields are displayed only if  the
              kernel was configured with various options; those dependencies are noted in the list.

              MemTotal %lu
                     Total usable RAM (i.e., physical RAM minus a few reserved bits and the kernel binary code).

              MemFree %lu
                     The sum of LowFree+HighFree.

              MemAvailable %lu (since Linux 3.14)
                     An estimate of how much memory is available for starting new applications, without swapping.

              Buffers %lu
                     Relatively  temporary storage for raw disk blocks that shouldn't get tremendously large (20MB or
                     so).

              Cached %lu
                     In-memory cache for files read from the disk (the page cache).  Doesn't include SwapCached.

              SwapCached %lu
                     Memory that once was swapped out, is swapped back in but still also is in the  swap  file.   (If
                     memory pressure is high, these pages don't need to be swapped out again because they are already
                     in the swap file.  This saves I/O.)

从其中可以看到:

  • Buffers是对原始磁盘块的临时存储,也就是用来缓存磁盘的数据,通常不会特别大(20M左右)。
  • Cached是从磁盘读取文件的页缓存,也就是用来缓存从文件读取的数据。这样,下次访问这些文件数据时,就可以直接从内存中快速读取,而不需要再次访问缓慢的磁盘。
  • SReclaimable 是Slab的一部分。Slab包括两部分,其中的可回收部分,用SReclimable记录;而不可回收部分,用 SUnreclaim 记录。

需要注意的是:

  • Buffer既可以用作“将要写入磁盘数据的缓存”,也可以用作“从磁盘读取数据的缓存”。
  • Cache既可以用作“从文件读取数据的页缓存”,也可以用作“写文件的页缓存”。

磁盘和文件的区别:

关于磁盘和文件的区别,磁盘是一个块设备,可以划分为不同的分区(LVM);在分区之上再创建文件系统,挂载到某个目录,之后才可以在这个目录中读写文件。

其实 Linux 中“一切皆文件”,而文章中提到的“文件”是普通文件,磁盘是块设备文件,这些大家可以执行 "ls -l <路径>" 查看它们的区别。

读写普通文件时,会经过文件系统,由文件系统负责与磁盘交互;而读写磁盘或者分区时,就会跳过文件系统,也就是所谓的“裸I/O”。这两种读写方式所使用的缓存是不通的,也就是Cache和Buffer的区别。

通常,读文件,首先经过 Block Buffer,然后到达 Page Cache。

。在老的Linux上这两个Cache是分开的。那这样对于文件数据,会被Cache两次。这种方案虽然简单,但低效。后期Linux把这两个Cache统一了。对于文件,Page Cache指向Block Buffer,对于非文件,则是Block Buffer。

给TA打赏
共{{data.count}}人
人已打赏
后端/架构存储架构

Atomic Read Modify Write Primitives for I/O Devices

2022-2-21 10:40:43

手机数码

雷蛇战锤狂鲨真无线专业版降噪耳机开卖 支持主动降噪和触控

2021-4-26 14:41:56

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索