通常,对应Linux的系统来说,可以通过cat /proc/meminfo查看内存的使用情况,Android是基于Linux的,所有也可以使用adb shell cat /proc/meminfo
查看内存的使用情况,如下所示:
MemTotal: 1893284 kB
MemFree: 823932 kB
Buffers: 30628 kB
Cached: 389332 kB
SwapCached: 0 kB
Active: 524500 kB
Inactive: 243048 kB
Active(anon): 347616 kB
Inactive(anon): 400 kB
Active(file): 176884 kB
Inactive(file): 242648 kB
Unevictable: 0 kB
Mlocked: 0 kB
HighTotal: 1149952 kB
HighFree: 259828 kB
LowTotal: 743332 kB
LowFree: 564104 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 347560 kB
Mapped: 96004 kB
Shmem: 452 kB
Slab: 45988 kB
SReclaimable: 22544 kB
SUnreclaim: 23444 kB
KernelStack: 6608 kB
PageTables: 8944 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 946640 kB
Committed_AS: 7674556 kB
VmallocTotal: 245760 kB
VmallocUsed: 74616 kB
VmallocChunk: 75616 kB
其中:
MemTotal: 总的可用内存
MemFree: 未被分配空闲的内存
Buffers: buffer的大小
Cached: cache的大小
SwapCached: swap 缓存的大小,Android很少使用swap的,经常为0
其中free memory = MemFree + Buffer + Cached,那么used memory = MemTotal - free memory = MemTotal - MemFree - Buffer - Cached,为649M
按理说应该是这样的,但事实却未必如此,使用procrank命令,看以看到总的信息如下:
PSS RSS
TOTAL 469252K 390792K
RAM: 1893284K total, 817792K free, 30692K buffers, 390192K cached, 452K shmem, 45976K slab
可用看到PSS的内存大小为469M,还有80M不知道跑哪去了。哦,完了还有PageTables占用的内存没有减去,而此时系统PageTables才8M,649 - 9 = 640M,
离469M的PSS还有71M,应该还有地方没有去除的。是的,shmem和slab没有去除,这两个加起来有46M,71 - 46 = 25M,那这25M去哪了呢?应该是Android
Reserved 或者是3D/HW buffer使用了吧!不太清楚。
其实,应该通过zoneinfo里面去查看内存的使用情况(单位:4kb,一个page的大小),可能更准确:
Node 0, zone Normal
pages free 140745
min 877
low 1096
high 1315
scanned 0
spanned 228352
present 192553
nr_free_pages 140745
nr_inactive_anon 0
nr_active_anon 0
nr_inactive_file 4187
nr_active_file 3590
nr_unevictable 0
nr_mlock 0
nr_anon_pages 0
nr_mapped 39
nr_file_pages 7771
nr_dirty 0
nr_writeback 0
nr_slab_reclaimable 5636
nr_slab_unreclaimable 5840
nr_page_table_pages 2479
nr_kernel_stack 867
nr_unstable 0
nr_bounce 0
nr_vmscan_write 0
nr_vmscan_immediate_reclaim 0
nr_writeback_temp 0
nr_isolated_anon 0
nr_isolated_file 0
nr_shmem 0
nr_dirtied 32731
nr_written 32731
nr_anon_transparent_hugepages 0
nr_free_cma 2329
protection: (0, 8902, 8902)
pagesets
cpu: 0
count: 52
high: 186
batch: 31
vm stats threshold: 8
all_unreclaimable: 0
start_pfn: 0
inactive_ratio: 1
Node 0, zone HighMem
pages free 58767
min 128
low 452
high 776
scanned 0
spanned 295936
present 284887
nr_free_pages 58767
nr_inactive_anon 101
nr_active_anon 90565
nr_inactive_file 57093
nr_active_file 41004
nr_unevictable 0
nr_mlock 0
nr_anon_pages 90557
nr_mapped 24040
nr_file_pages 98210
nr_dirty 0
nr_writeback 0
nr_slab_reclaimable 0
nr_slab_unreclaimable 0
nr_page_table_pages 0
nr_kernel_stack 0
nr_unstable 0
nr_bounce 0
nr_vmscan_write 0
nr_vmscan_immediate_reclaim 0
nr_writeback_temp 0
nr_isolated_anon 0
nr_isolated_file 0
nr_shmem 113
nr_dirtied 36767
nr_written 36179
nr_anon_transparent_hugepages 0
nr_free_cma 18652
protection: (0, 0, 0)
pagesets
cpu: 0
count: 14
high: 186
batch: 31
vm stats threshold: 10
all_unreclaimable: 0
start_pfn: 228352
inactive_ratio: 3
可以看到只有一个node,但有两个zone:Normal和HighMem,Android一般不使用DMA,所以只有两个zone。
User space usage = nr_inactive_anon + nr_active_anon + nr_inactive_file + nr_active_file + nr_unevictable
Kernel usage = present - nr_free_pages - User space usage
分别计算出两个zone的 User space usage 和Kernel usage,然后再通过
Reserved + 3D/HW buffer = Total memory - Kernel usage - User space usage
得到Reserved + 3D/HW buffer的大小。
查看内存的使用情况,如下所示:
MemTotal: 1893284 kB
MemFree: 823932 kB
Buffers: 30628 kB
Cached: 389332 kB
SwapCached: 0 kB
Active: 524500 kB
Inactive: 243048 kB
Active(anon): 347616 kB
Inactive(anon): 400 kB
Active(file): 176884 kB
Inactive(file): 242648 kB
Unevictable: 0 kB
Mlocked: 0 kB
HighTotal: 1149952 kB
HighFree: 259828 kB
LowTotal: 743332 kB
LowFree: 564104 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 347560 kB
Mapped: 96004 kB
Shmem: 452 kB
Slab: 45988 kB
SReclaimable: 22544 kB
SUnreclaim: 23444 kB
KernelStack: 6608 kB
PageTables: 8944 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 946640 kB
Committed_AS: 7674556 kB
VmallocTotal: 245760 kB
VmallocUsed: 74616 kB
VmallocChunk: 75616 kB
其中:
MemTotal: 总的可用内存
MemFree: 未被分配空闲的内存
Buffers: buffer的大小
Cached: cache的大小
SwapCached: swap 缓存的大小,Android很少使用swap的,经常为0
其中free memory = MemFree + Buffer + Cached,那么used memory = MemTotal - free memory = MemTotal - MemFree - Buffer - Cached,为649M
按理说应该是这样的,但事实却未必如此,使用procrank命令,看以看到总的信息如下:
PSS RSS
TOTAL 469252K 390792K
RAM: 1893284K total, 817792K free, 30692K buffers, 390192K cached, 452K shmem, 45976K slab
可用看到PSS的内存大小为469M,还有80M不知道跑哪去了。哦,完了还有PageTables占用的内存没有减去,而此时系统PageTables才8M,649 - 9 = 640M,
离469M的PSS还有71M,应该还有地方没有去除的。是的,shmem和slab没有去除,这两个加起来有46M,71 - 46 = 25M,那这25M去哪了呢?应该是Android
Reserved 或者是3D/HW buffer使用了吧!不太清楚。
其实,应该通过zoneinfo里面去查看内存的使用情况(单位:4kb,一个page的大小),可能更准确:
Node 0, zone Normal
pages free 140745
min 877
low 1096
high 1315
scanned 0
spanned 228352
present 192553
nr_free_pages 140745
nr_inactive_anon 0
nr_active_anon 0
nr_inactive_file 4187
nr_active_file 3590
nr_unevictable 0
nr_mlock 0
nr_anon_pages 0
nr_mapped 39
nr_file_pages 7771
nr_dirty 0
nr_writeback 0
nr_slab_reclaimable 5636
nr_slab_unreclaimable 5840
nr_page_table_pages 2479
nr_kernel_stack 867
nr_unstable 0
nr_bounce 0
nr_vmscan_write 0
nr_vmscan_immediate_reclaim 0
nr_writeback_temp 0
nr_isolated_anon 0
nr_isolated_file 0
nr_shmem 0
nr_dirtied 32731
nr_written 32731
nr_anon_transparent_hugepages 0
nr_free_cma 2329
protection: (0, 8902, 8902)
pagesets
cpu: 0
count: 52
high: 186
batch: 31
vm stats threshold: 8
all_unreclaimable: 0
start_pfn: 0
inactive_ratio: 1
Node 0, zone HighMem
pages free 58767
min 128
low 452
high 776
scanned 0
spanned 295936
present 284887
nr_free_pages 58767
nr_inactive_anon 101
nr_active_anon 90565
nr_inactive_file 57093
nr_active_file 41004
nr_unevictable 0
nr_mlock 0
nr_anon_pages 90557
nr_mapped 24040
nr_file_pages 98210
nr_dirty 0
nr_writeback 0
nr_slab_reclaimable 0
nr_slab_unreclaimable 0
nr_page_table_pages 0
nr_kernel_stack 0
nr_unstable 0
nr_bounce 0
nr_vmscan_write 0
nr_vmscan_immediate_reclaim 0
nr_writeback_temp 0
nr_isolated_anon 0
nr_isolated_file 0
nr_shmem 113
nr_dirtied 36767
nr_written 36179
nr_anon_transparent_hugepages 0
nr_free_cma 18652
protection: (0, 0, 0)
pagesets
cpu: 0
count: 14
high: 186
batch: 31
vm stats threshold: 10
all_unreclaimable: 0
start_pfn: 228352
inactive_ratio: 3
可以看到只有一个node,但有两个zone:Normal和HighMem,Android一般不使用DMA,所以只有两个zone。
User space usage = nr_inactive_anon + nr_active_anon + nr_inactive_file + nr_active_file + nr_unevictable
Kernel usage = present - nr_free_pages - User space usage
分别计算出两个zone的 User space usage 和Kernel usage,然后再通过
Reserved + 3D/HW buffer = Total memory - Kernel usage - User space usage
得到Reserved + 3D/HW buffer的大小。