目录
- 前言
- 一、Android查看内存相关信息的方法
- 1.1 通过 adb shell 获取内存信息
- 1.2 通过编程方式获取内存信息
- 1.3 adb shell 获取应用程序内存使用情况
- 1.4 free指令
- 二、总结
前言
一、Android查看内存相关信息的方法
1.1 通过 adb shell 获取内存信息
C:\Users\henry.xue>adb shell
trinket:/ # cat proc/meminfo
MemTotal: 3711580 kB
MemFree: 607128 kB
MemAvailable: 2371444 kB
Buffers: 10980 kB
Cached: 1697644 kB
SwapCached: 0 kB
Active: 1107552 kB
Inactive: 1345780 kB
Active(anon): 745760 kB
Inactive(anon): 4916 kB
Active(file): 361792 kB
Inactive(file): 1340864 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Dirty: 2320 kB
Writeback: 0 kB
AnonPages: 744756 kB
Mapped: 666316 kB
Shmem: 5960 kB
Slab: 231564 kB
SReclaimable: 70964 kB
SUnreclaim: 160600 kB
KernelStack: 35616 kB
PageTables: 52180 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 3952936 kB
Committed_AS: 90872212 kB
VmallocTotal: 263061440 kB
VmallocUsed: 75784 kB
VmallocChunk: 0 kB
CmaTotal: 208896 kB
CmaFree: 0 kB
参数详解:
1.2 通过编程方式获取内存信息
ActivityManager里的内部类MemoryInfo
public static class MemoryInfo implements Parcelable {
/**
* The available memory on the system. This number should not
* be considered absolute: due to the nature of the kernel, a significant
* portion of this memory is actually in use and needed for the overall
* system to run well.
*/
public long availMem;
/**
* The total memory accessible by the kernel. This is basically the
* RAM size of the device, not including below-kernel fixed allocations
* like DMA buffers, RAM for the baseband CPU, etc.
*/
public long totalMem;
/**
* The threshold of {@link #availMem} at which we consider memory to be
* low and start killing background services and other non-extraneous
* processes.
*/
public long threshold;
/**
* Set to true if the system considers itself to currently be in a low
* memory situation.
*/
public boolean lowMemory;
......
其中重要的四个参数:
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
long totalMemory = memoryInfo.totalMem;
long availableMemory = memoryInfo.availMem;
这段代码将获取设备的总内存和可用内存信息。
1.3 adb shell 获取应用程序内存使用情况
- 使用以下命令可以查看应用程序的内存使用情况:
- 将 <package_name> 替换为您要查看的应用程序包名,这将返回该应用程序的内存使用情况,包括堆内存、Native 内存等。
示例:
trinket:/ # dumpsys meminfo com.android.systemui
Applications Memory Usage (in Kilobytes):
Uptime: 85930415 Realtime: 85930415
** MEMINFO in pid 2027 [com.android.systemui] **
Pss Private Private Swap Heap Heap Heap
Total Dirty Clean Dirty Size Alloc Free
------ ------ ------ ------ ------ ------ ------
Native Heap 27358 27308 0 0 33420 29399 4020
Dalvik Heap 6052 6008 0 0 11031 5516 5515
Dalvik Other 2273 2268 0 0
Stack 40 40 0 0
Ashmem 96 92 0 0
Gfx dev 4332 4332 0 0
Other dev 44 0 40 0
.so mmap 2982 88 76 0
.jar mmap 2108 0 192 0
.apk mmap 14337 0 11888 0
.ttf mmap 196 0 52 0
.dex mmap 173 4 168 0
.oat mmap 952 0 0 0
.art mmap 1583 1280 0 0
Other mmap 1645 476 108 0
EGL mtrack 3024 3024 0 0
GL mtrack 2964 2964 0 0
Unknown 1548 1544 0 0
TOTAL 71707 49428 12524 0 44451 34915 9535
App Summary
Pss(KB)
------
Java Heap: 7288
Native Heap: 27308
Code: 12468
Stack: 40
Graphics: 10320
Private Other: 4528
System: 9755
TOTAL: 71707 TOTAL SWAP (KB): 0
Objects
Views: 840 ViewRootImpl: 6
AppContexts: 16 Activities: 0
Assets: 14 AssetManagers: 0
Local Binders: 171 Proxy Binders: 67
Parcel memory: 22 Parcel count: 75
Death Recipients: 4 OpenSSL Sockets: 0
WebViews: 0
SQL
MEMORY_USED: 0
PAGECACHE_OVERFLOW: 0 MALLOC_SIZE: 0
trinket:/ #
在输出中,各个部分的含义如下:
App Summary
Objects
SQL
1.4 free指令
free -m
xxxxxxx:/ # free -m
total used free shared buffers
Mem: 3624 3319 304 7 17
-/+ buffers/cache: 3302 322
Swap: 2047 0 2047
trinket:/ #
free 后面还可以跟其他参数:
使用watch 命令来定期执行 free 命令并显示系统内存使用情况
watch -n 1 free -m
这个命令将每隔 1 秒执行一次 free -m 命令,并在终端中显示当前的内存使用情况(以 MB 为单位)。可以根据需要更改 -n 参数的值来调整更新频率。
二、总结
- adb shell cat proc/meminfo
- ActivityManager.MemoryInfo
- adb shell dumpsys meminfo < package_name>
- adb shell free -m