diff --git "a/_posts/2024-10-21-Linux-\345\206\205\345\255\230\347\256\241\347\220\206\350\246\201\347\220\206\350\247\243\347\232\204\346\246\202\345\277\265.md" "b/_posts/2024-10-21-Linux-\345\206\205\345\255\230\347\256\241\347\220\206\350\246\201\347\220\206\350\247\243\347\232\204\346\246\202\345\277\265.md" new file mode 100644 index 0000000..ae9079b --- /dev/null +++ "b/_posts/2024-10-21-Linux-\345\206\205\345\255\230\347\256\241\347\220\206\350\246\201\347\220\206\350\247\243\347\232\204\346\246\202\345\277\265.md" @@ -0,0 +1,172 @@ +--- +title: 内存分析需要理解的几个概念 +date: 2024-10-21 +0800 # 2022-01-01 13:14:15 +0800 只写日期也行;不写秒也行;这样也行 2022-03-09T00:55:42+08:00 +categories: [linux] +tags: [linux, perf] # TAG names should always be lowercase + +# 以下默认false +math: true +mermaid: true +# pin: true +--- + +![top_memory_info](/assets/images/perf/20241021_top_memo_usage/top_mem_screen_shot.png) + +“真实的” 内存空闲率 = (free + shared + buffers + cached)/ Total = 5860 M / 7983M X 100 % = 73.4 % + +## 1. Linux内存分类 ## + +* `匿名内存`:存储用户计算过程中间的数据,与物理磁盘上的文件无关; +* `File-Backed内存`:用作磁盘高速缓存,其物理内存与物理磁盘上的文件对应; + +### 1.1 `Shmem` ### + +包括: + +* `shared memory`. +* `tmpfs`:所使用的内存(基于内存的文件系统)。`Linux`将空闲内存用于缓存,并且在需要的时候,回收这些内存。 +* `devtmpfs`:指的是`/dev`文件系统,`/dev`目录下所有的文件占用的空间也属于共享内存。 + +### 1.2 `buffers` & `cache` ### + +1. `buffers`: 用于`块设备I/O`缓存; +2. `cached`:`page cache`,用于文件系统(读取文件时,kernel创建的缓存); + +使用`top`命令查看时,`buffers`与`cached`通常合并在一起显示,他们都是`kernel`使用的缓存。在需要的时候,可以被回收。 + +## 2. 查看内存使用情况 ## + +### 2.1 内存利用率详细信息 ### + +```bash +cat /proc/meminfo + +MemTotal: 8174352 kB +MemFree: 376952 kB +Buffers: 527412 kB +Cached: 5178924 kB +SwapCached: 60 kB +Active: 3061760 kB +Inactive: 4066588 kB +Active(anon): 1112780 kB +Inactive(anon): 314156 kB +Active(file): 1948980 kB +Inactive(file): 3752432 kB +Unevictable: 6724 kB +Mlocked: 6724 kB +SwapTotal: 16779884 kB +SwapFree: 16777400 kB +Dirty: 376 kB +Writeback: 0 kB +AnonPages: 1428844 kB +Mapped: 64632 kB +Shmem: 644 kB +Slab: 557384 kB +SReclaimable: 338272 kB +SUnreclaim: 219112 kB +KernelStack: 4024 kB +PageTables: 12440 kB +NFS_Unstable: 0 kB +Bounce: 0 kB +WritebackTmp: 0 kB +CommitLimit: 20867060 kB +Committed_AS: 2406484 kB +VmallocTotal: 34359738367 kB +VmallocUsed: 111536 kB +VmallocChunk: 34359455060 kB +HugePages_Total: 0 +HugePages_Free: 0 +HugePages_Rsvd: 0 +HugePages_Surp: 0 +Hugepagesize: 2048 kB +DirectMap4k: 6384 kB +DirectMap2M: 2080768 kB +DirectMap1G: 6291456 kB +``` + +### 2.2 查看进程内存占用 ### + +有用的信息: + +* `VmData`:data段大小; +* `VmExe`:text段大小; +* `Vmlib`:共享库占用内存空间大小; +* `VmRSS`:物理内存使用量; +* `VMSwap`:交换分区使用量; + +```bash +cat /proc/30029/status +Name: java +State: S (sleeping) +Tgid: 30029 +Pid: 30029 +PPid: 29983 +TracerPid: 0 +Uid: 54322 54322 54322 54322 +Gid: 54323 54323 54323 54323 +FDSize: 8192 +Groups: 10 54323 +VmPeak: 2754032 kB +VmSize: 2678836 kB +VmLck: 0 kB +VmHWM: 1337912 kB +VmRSS: 1337512 kB +VmData: 2575692 kB +VmStk: 1012 kB +VmExe: 60 kB +VmLib: 101564 kB +VmPTE: 3048 kB +Threads: 98 +SigQ: 0/63825 +SigPnd: 0000000000000000 +ShdPnd: 0000000000000000 +SigBlk: 0000000000000004 +SigIgn: 0000000000000001 +SigCgt: 1000000180005cce +CapInh: 0000000000000000 +CapPrm: 0000000000000000 +CapEff: 0000000000000000 +CapBnd: ffffffffffffffff +Cpus_allowed: ffffffff +Cpus_allowed_list: 0-31 +Mems_allowed: 00000000, +Mems_allowed_list: 0 +voluntary_ctxt_switches: 12468 +nonvoluntary_ctxt_switches: 19 +``` + +### 2.3 pmap 命令 ### + +执行 `ps aux` 后输出的各进程的 RSS (resident set size), 表示进程占用内存的大小,单位是KB。 需要注意的是, `RSS` 值实际上是基于 `pmap` 命令,表示`该进程正在使用的物理内存的总和`。 + +```bash +pmap -x 30029 +30029: /slview/jdk150/jdk1.5.0_06/bin/java -com.apache.Test +Address Kbytes RSS Dirty Mode Mapping +0000000008048000 60 48 0 r-x-- java +0000000008057000 8 8 8 rwx-- java +0000000009f1d000 23184 23140 23140 rwx-- [ anon ] +000000004d1f1000 108 96 0 r-x-- ld-2.5.so +000000004d20c000 4 4 4 r-x-- ld-2.5.so +000000004d20d000 4 4 4 rwx-- ld-2.5.so +000000004d214000 1356 548 0 r-x-- libc-2.5.so +000000004d367000 8 8 8 r-x-- libc-2.5.so +00007f581e51d000 16 16 0 r--s- huanan-product-2.6.1-snapshots.jar +00007f581e521000 24 24 0 r--s- dt.jar +00007f581e527000 36 36 0 r--s- gnome-java-bridge.jar +00007f581e530000 32 32 8 rw-s- 13228 +00007f581e538000 4 4 4 rw--- [ anon ] +00007f581e539000 4 4 0 r---- [ anon ] +00007f581e53a000 8 8 8 rw--- [ anon ] +00007fffe9eb7000 84 32 32 rw--- [ stack ] +00007fffe9fff000 4 4 0 r-x-- [ anon ] +ffffffffff600000 4 0 0 r-x-- [ anon ] +(部分省略) +---------------- ------ ------ ------ +total kB 2484196 36180 26880 +``` + +## 3. 参考资料 ## + +* [How Linux Works(三): 内存管理](https://github.com/riboseyim/ebook-linuxperfmaster/blob/master/chapter/kernel/Linux-Works-Memory.md) +* [Linux 0.11 源码阅读笔记-内存的基础概念](https://draapho.github.io/2017/01/26/1704-linux-source1/) diff --git a/assets/images/perf/20241021_top_memo_usage/top_mem_screen_shot.png b/assets/images/perf/20241021_top_memo_usage/top_mem_screen_shot.png new file mode 100644 index 0000000..ae2c7df Binary files /dev/null and b/assets/images/perf/20241021_top_memo_usage/top_mem_screen_shot.png differ