Linux Userspace Kernelspace内存分布

    最近又分析嵌入式Linux系统内存优化,分阶段更新.

meminfo详解 meminfo解析 

这篇文章介绍了meminfo的详细信息,可以参阅:

https://blog.csdn.net/axw2013/article/details/79645422

 

History:

2020-12-30:更新Meminfo各个字段的解释

  • 查看系统内存:
1 sync
2 echo 3 > /proc/sys/vm/drop_caches
3 cat /proc/meminfo

sync是为了将buffers里面的内容存盘;drop cache是为了看到准确的内存信息。

       /proc/sys/vm/drop_caches (since Linux 2.6.16)
              Writing to this file causes the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.

              To free pagecache, use echo 1 > /proc/sys/vm/drop_caches; to free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches; to free pagecache, dentries and inodes, use echo 3 > /proc/sys/vm/drop_caches.

              Because this is a non-destructive operation and dirty objects are not freeable, the user should run sync(8) first.

 

MemTotal:          99348 kB    #可使用的总内存
MemFree:           22272 kB    #当前可用的内存
Buffers:             976 kB    #块设备缓存
Cached:             6220 kB    #文件缓存:各个进程打开的文件就放这里
SwapCached:            0 kB    
Active:            23612 kB    #Active(anon) + Active(file):最近被频繁使用的内存
Inactive:           4460 kB    #Inactive(anon) + Inactive(file):最近没有被使用的内存
Active(anon):      20892 kB    #tmpfs,共享内存,进程的堆、栈,进程通过mmap/brk等获取的内存
Inactive(anon):      784 kB    #同
Active(file):       2720 kB    #文件缓存,就是各个进程打开的文件,从磁盘缓存到内存中,而且最近在被使用
Inactive(file):     3676 kB    #最新加载的文件内容,或者处于待回收状态的文件缓存
Unevictable:           0 kB    #进程空间中上锁的内存
Mlocked:               0 kB    #同上Unevictable
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:                 8 kB    #一般是指需要存盘的数据,文件被改写但是没有存盘
Writeback:             0 kB
AnonPages:         20948 kB
Mapped:             3672 kB    #映射到进程内存中的内存大小,/proc/$pid/maps里面的一些分布
Shmem:               784 kB    #共享内存和tmpfs
Slab:              31188 kB    #Salb = SReclaimable + SUnreclaim,Kernel space缓存的数据,和CPU L1/L2/L3缓存不是一个概念
SReclaimable:       1336 kB    #可回收的slab
SUnreclaim:        29852 kB    #不可回收
KernelStack:        2592 kB    #Kernel栈空间的大小
PageTables:          824 kB    #内存管理MM系统中页表消耗的内存
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:       49672 kB
Committed_AS:      32732 kB
VmallocTotal:    1032116 kB
VmallocUsed:       16948 kB
VmallocChunk:     976480 kB

 

  •  使用pidstat查看进程占用的内存信息

pidstat -r

RSS列为实际使用的内存,共享库部分会重复计算,因此相对可信。

20:36:32      UID       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
20:36:32        0         1      0.03      0.00    3088    544   0.55  init
20:36:32        0      1135      0.47      0.00    1664    912   0.92  thttpd
20:36:32        0      1153      0.01      0.00    3076    356   0.36  klogd

 

  • 查看kernel modules占用的内存 /proc/modules

可以通过lsmod或者/proc/modules看各个kernel module占用的内存,注意单位转换。

 

通过上面的方法,可以计算出内存的粗略分布情况:

Userspace Applications: 34.18 MB
Kernel Modules: 13.19 MB
Slab: 30.45 MB

total used: 34.18 + 13.19 + 30.45 = 77.82

Free: 21.75

 

  • 选择优化对象: 擒贼先擒王,挑占用内存的大头下手。
  • 后面再介绍内存优化的方法。

 

posted @ 2019-10-25 15:05  xinpengc  阅读(504)  评论(0编辑  收藏  举报