Linux内存FAQ

597 阅读4分钟

什么是VSS、RSS、PSS、USS?如何查看进程的VSS、RSS、PSS、USS?

  • VSS(Virtual Set Size):虚拟内存用量(包含共享库占用的全部内存、已分配未使用的内存),基本不用于判断内存使用量。
  • RSS(Resident Set Size):实际使用的物理内存,但是包含了共享库占用的内存,共享库仅被加载一次,所有使用到这个共享库的进程都会在RSS中将用到的共享库内存计算进去,并不能完全反应进程的内存用量,存在一定误导。
  • PSS(Proportional Set Size):实际使用的物理内存(按进程数等比例分配共享库占用内存);假设有2个进程使用了同个共享库,该共享库占用了30页内存,那么PSS统计认为每个进程分别占用该共享库15页,该指标能较为准确的反映进程的资源用量。
  • USS(Unique Set Size):进程独自占用的物理内存,不包含共享库占用的内存;该指标能准确反映出内存用量,当进程存在内存泄漏时,USS是最佳观测数据。

VSS >= RSS >= PSS >= USS

Linux系统可以通过smem命令来查看进程的VSS、RSS、PSS、USS

# yum -y install smem

image.png

/proc/[PID]/smaps中记录了该进程使用的VSS、RSS、PSS、USS的信息,如下所示:

  • RSS对应smaps中的Rss的sum值
  • PSS对应smaps中的Pss的sum值
  • VSS对应smaps中的Size的sum值
  • USS对应smaps中的Private_CleanPrivate_Dirty的sum值

image.png

Buffer VS Cache

Buffer传输.jpg

  • Buffer:缓冲区是对块设备的临时存储,即写入磁盘的缓存数据,通常不是很大(20M左右)。通过这种方式内核可以将分散的写操作集合起来统一优化磁盘写操作;

Cache传输.jpg

  • Cache:(Page Cache)缓存是一个用于磁盘上读取文件的页面缓存,它用于缓存从文件中读取的数据,这样下次访问这些文件数据时可以直接从内存中快速获取;

free命令的man文档中对buffer和cache的定义

buffers
      Memory used by kernel buffers (Buffers in /proc/meminfo)

cache  
      Memory used by the page cache and slabs (Cached and SReclaimable in /proc/meminfo)

buff/cache
      Sum of buffers and cache

/proc中对buffer和cache的定义

Buffers %lu
     Relatively temporary storage for raw disk blocks
     that shouldn't get tremendously large (20 MB 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.)
Slab %lu
     In-kernel data structures cache.  (See
     slabinfo(5).)

SReclaimable %lu (since Linux 2.6.19)
     Part of Slab, that might be reclaimed, such as
     caches.
Cache for Writing
  • 清理缓存
# free -m
              total        used        free      shared  buff/cache   available
Mem:         124575       98619        5914         201       20041       26325
Swap:             0           0           0

# sync

# echo 3 > /proc/sys/vm/drop_caches 

# free -m
              total        used        free      shared  buff/cache   available
Mem:         124575       98359       23622         201        2593       26584
Swap:             0           0           0

/proc/sys/vm/drop_caches

  • 开启另外一个终端执行vmstat 2

image.png bibo分别代表块设备读写的大小,单位是block/s;由于linux中的块大小是1KB,这个单位相当于KB/s

  • 接下来向文件中写入数据
# dd if=/dev/urandom of=/tmp/file bs=1M count=500

从vmstat结果可以看出cache持续增长但是buffer基本保持不变

image.png

Buffer for Reading
  • 清理缓存

image.png

  • 开启一个vmstat监控窗口

  • 通过find命令来测试

# find / -type f 

image.png

可以看出来buffer和cache都有增长,但是buffer增速更快

小结

Buffer既可以作为写入磁盘的数据的缓存,可以作为从磁盘读取数据的缓存;

Cache既可以作为从文件中读取数据的页面缓存,也可以作为写入文件的页面缓存;

如何查看进程的Buffer和Cache

hcache使用这个工具可以查看进程的buffer和cache信息

image.png

RSS包含buffer、cache么?

为什么容器cAdvisor监控输出的数据比直接使用top看到的容器进程占用资源小的多?

什么是malloc?有啥用?

/proc/[PID]/status中的RssAnonRssFileRssShmem什么意思?