现在的位置: 首页 > 综合 > 正文

Android手机总内存和可用内存

2013年08月07日 ⁄ 综合 ⁄ 共 8656字 ⁄ 字号 评论关闭

参考:http://zhang247124629.iteye.com/blog/1536300http://www.redhat.com/advice/tips/meminfo.html

在android开发中,有时候我们想获取手机的一些硬件信息,比如android手机的总内存和可用内存大小。这个该如何实现呢?

通过读取文件"/proc/meminfo"的信息能够获取手机Memory的总量,而通过ActivityManager.getMemoryInfo(ActivityManager.MemoryInfo)方法可以获取当前的可用Memory量。
      "/proc/meminfo"文件记录了android手机的一些内存信息,在命令行窗口里输入"adb shell",进入shell环境,输入"cat /proc/meminfo"即可在命令行里显示meminfo文件的内容,具体如下所示。 

1 内存信息查看

proc 文件系统中涉及内存使用情况的文件 主要有:

/proc/meminfo 表征了系统内存使用概要信息:

[root@localhost ~]# cat /proc/meminfo

MemTotal:       505964 kB              // 系统可用物理内存总量

MemFree:         16060 kB             // 系统空闲内物理内存总量 = HighFree+ LowFree

Buffers:         41132 kB                // 系统分配但未被使用的 buffer 数量 ( 注 1)

Cached:         359268 kB              // 系统分配但未被使用的 cache 数量 ( 注 1)

SwapCached:          0 kB             // 指交换出去的内存,再次交换回来,但在 swap 文件中仍存在,主要为了节约这块内存再次交换出去的 IO 操作

Active:         137156 kB               // 最近使用的内存,除非必要,一般不会立即回收

Inactive:       282600 kB                 // 最近未使用的内存,回收时优先回收

HighTotal:           0 kB                // 高端内存总量 (High Zone) , 860M 以上的物理内存

HighFree:            0 kB               // 高端内存空闲总量

LowTotal:       505964 kB              // 常规内存总量 (Normal Zone) ,可被 kernel 和应用任意使用

LowFree:         16060 kB             // 常规内存空闲总量

SwapTotal:     1269124 kB               // 总的交换内存大小

SwapFree:      1268948 kB              // 空闲的交换内存大小

Dirty:              76 kB               // 需要写回磁盘的数据大小

Writeback:           0 kB               // 正在写回磁盘的数据大小

Mapped:          33808 kB             //mmap 使用的内存大小

Slab:            63204 kB               //slab pool 大小

CommitLimit:   1522104 kB        // 系统实际可分配内存总量 ( 注 2)

Committed_AS:    98172 kB             // 系统当前已分配的内存总量,包括 unused( 注 2)

PageTables:       1660 kB                // 管理内存页表所使用的内存

VmallocTotal:   507896 kB                //vmalloc 可使用的总内存大小

VmallocUsed:      3676 kB        // vmalloc 已用的总内存大小

VmallocChunk:   503960 kB              //vmalloc 可分配的最大的逻辑连续的内存大小

....

(1) 、 cache 与 buffer 的区别

Cache 和 buffer 都是系统做为缓冲的内存, cache 指的是 page cache ,表示文件系统的 cache ;而 buffer指的是 buffer cache ,用来为块设备的做读写缓冲,与具体的块设备关联,跟踪块的变化。 Linux 系统之所以使用 cache 和 buffer ,主要是为了提高文件读写访问的性能。

简单来说, buffers 是用来存储,目录里面有什么内容,权限等等。而 cached 直接用来记忆我们打开的文件。如果 cache 的值很大,说明 cache 住的文件数很多。

Cache 和 buffer 的内存对kernel 来说是已经被使用的 , free 的内存是指不需要任何处理即可分配的内存。对应用程序而言,当 free 内存不足时,可以从 buffer 和 cache 中回收内存再次分配。所以对 kernel 而言,可用内存即为 free ,对应用程序,可用内存为 free+cache+buffer 。

(2) 、 CommitLimit 和 Committed_AS

CommitLimit :基于过量分配比率 (vm.overcommit_ratio) ,系统可分配的内存总量大小。这个值只有当采用严格过量内存管理 (vm.overcommit_memory=2) 时才有效。其总大小的计划公式如下:

CommitLimit = (vm.overcommit_ratio * Physical RAM) + Swap

Committed_AS :系统当前已分配的内存总量,包括已分配但尚未使用的内存大小。如,一个进程分配了 1G 的内存,有 300M 在使用,但对 VM 已分配了 1G ,而且这 1G 在任何时候都可以被使用。当(vm.overcommit_memory=2) 时, Committed_AS 不能大于 CommitLimit 。

/proc/pid /status 表征了进程状态信息:

[zhangyan@localhost opt]$ cat /proc/28151/status

Name:   memtest

State:  R (running)

SleepAVG:       0%

......

Groups: 500

VmSize:     7528 kB   // 该进程使用虚拟内存大小,是 VmLib, VmExe, VmData 和 VmStk 的总和

VmLck:         0 kB  // 进程当前使用的并且加锁的虚拟内存总数

VmRSS:      5400 kB // 占用的在物理内存大小,它没有交换到硬盘,包括代码,数据和栈

VmData:     5136 kB  // 堆使用的虚拟内存

VmStk:      1104 kB   // 栈使用的虚拟内存

VmExe:         1 kB  // 可执行的和静态链接库所使用的虚拟内存

VmLib:       1259 kB   // 动态链接库所使用的虚拟内存

......

/proc/slabinfo 主要表征了 slab pool 内数据的信息:

[root@localhost ~]# cat /proc/slabinfo

slabinfo - version: 2.0

# name            <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <batchcount> <limit> <sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail>

nfs_direct_cache       0      0     68   58    1 : tunables  120   60    8 : slabdata      0     0      0

......

主要关注 <active_objs> <num_objs> <objsize> 三个项,一种类型的数据所占内存的大小为: active_objs* objsize ,该类内存池中空闲内存数: (num_objs- active_objs)* objsize 。但并不是所有的空闲的内存单元是可回收的,这与创建时该类型内存池时的属性相关。

2 内存管理策略

    与内存相关的配置都在 /proc/sys/vm 目录下 

/proc/sys/vm/dirty_ratio

文件系统写缓冲区的大小,单位是百分比,表示系统内存的百分比,表示当写缓冲使用到系统内存多少的时候,开始向磁盘写出数据。增大之会使用更多系统内存用于磁盘写缓冲,也可以极大提高系统的写性能。但是,当需要持续、恒定的写入时,应该降低其数值。

/proc/sys/vm/dirty_background_ratio

控制 pdflush 进程在何时刷新磁盘。单位是百分比,表示系统内存的百分比,意思是当写缓冲使用到系统内存多少的时候, pdflush 开始向磁盘写出数据。增大之会使用更多系统内存用于磁盘写缓冲,也可以极大提高系统的写性能。但是,当需要持续、恒定的写入场合时,应该降低其数值。

/proc/sys/vm/dirty_writeback_centisecs

控制 pdflush 的运行间隔。单位是 1/100 秒。缺省数值是 500 ,也就是 5 秒。如果你的系统是持续地写入动作,那么实际上还是降低这个数值比较好,这样可以把尖峰的写操作削平成多次写操作。

/proc/sys/vm/dirty_expire_centisecs

表示如果脏数据在内存中驻留时间超过该值, pdflush 进程在下一次将把这些数据写回磁盘。单位是 1/100 秒。缺省是 30000 ,也就是 30 秒的数据就算旧了,将会刷新磁盘。

/proc/sys/vm/overcommit_memory

指定内核针对内存分配的策略,其值可以是 0  1  2 

 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。

 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。

 表示内核允许分配所有的物理内存的一定百分比与交换空间总和之内的内存(参见 overcommit_ratio )。

/proc/sys/vm/overcommit_ratio

如果 overcommit_memory=2 ,可以过载内存的百分比。系统可分配内存 = 交换空间 + 物理内存*overcommit_ratio/100 

--------------------------------------------------------------------------------------

Tips & Tricks

Featured Article: /proc/meminfo Explained


March 2003


"Free," "buffer," "swap," "dirty." What does it all mean? If you said, "something to do with the Summer of '68", you may need a primer on 'meminfo'.

The entries in the /proc/meminfo can help explain what's going on with your memory usage, if you know how to read it.

Example of "cat /proc/meminfo":

root:	total:    	used:    	free:  		shared:	buffers:	cached:
Mem:  	1055760384	1041887232	13873152	0	100417536 	711233536
Swap: 	1077501952  	8540160 	1068961792
						
MemTotal:		1031016 kB	
MemFree:		13548 kB
MemShared:		0 kB
Buffers:		98064 kB
Cached:			692320 kB
SwapCached:		2244 kB
Active:			563112 kB
Inact_dirty:		309584 kB
Inact_clean:		79508 kB
Inact_target:		190440 kB
HighTotal:		130992 kB
HighFree:		1876 kB
LowTotal:		900024 kB
LowFree:		11672 kB
SwapTotal:		1052248 kB
SwapFree:		1043908 kB
Committed_AS:		332340 kB
						

The information comes in the form of both high-level and low-level statistics. At the top you see a quick summary of the most common values people would like to look at. Below you find the individual values we will discuss. First we will discuss the high-level
statistics.

High-Level Statistics

  • MemTotal: Total usable ram (i.e. physical ram minus a few reserved bits and the kernel binary code)
  • MemFree: Is sum of LowFree+HighFree (overall stat)
  • MemShared: 0; is here for compat reasons but always zero.
  • Buffers: Memory in buffer cache. mostly useless as metric nowadays
  • Cached: Memory in the pagecache (diskcache) minus SwapCache
  • SwapCache: Memory that once was swapped out, is swapped back in but still also is in the swapfile (if memory is needed it doesn't need to be swapped out AGAIN because it is already in the swapfile. This saves I/O)

Detailed Level Statistics
VM Statistics

VM splits the cache pages into "active" and "inactive" memory. The idea is that if you need memory and some cache needs to be sacrificed for that, you take it from inactive since that's expected to be not used. The vm checks what is used on a regular basis
and moves stuff around.

When you use memory, the CPU sets a bit in the pagetable and the VM checks that bit occasionally, and based on that, it can move pages back to active. And within active there's an order of "longest ago not used" (roughly, it's a little more complex in reality).
The longest-ago used ones can get moved to inactive. Inactive is split into two in the above kernel (2.4.18-24.8.0). Some have it three.

  • Active: Memory that has been used more recently and usually not reclaimed unless absolutely necessary.
  • Inact_dirty: Dirty means "might need writing to disk or swap." Takes more work to free. Examples might be files that have not been written to yet. They aren't written to memory too soon in order to keep the I/O down. For instance, if you're
    writing logs, it might be better to wait until you have a complete log ready before sending it to disk.
  • Inact_clean: Assumed to be easily freeable. The kernel will try to keep some clean stuff around always to have a bit of breathing room.
  • Inact_target: Just a goal metric the kernel uses for making sure there are enough inactive pages around. When exceeded, the kernel will not do work to move pages from active to inactive. A page can also get inactive in a few other ways, e.g.
    if you do a long sequential I/O, the kernel assumes you're not going to use that memory and makes it inactive preventively. So you can get more inactive pages than the target because the kernel marks some cache as "more likely to be never used" and lets it
    cheat in the "last used" order.

Memory Statistics

  • HighTotal: is the total amount of memory in the high region. Highmem is all memory above (approx) 860MB of physical RAM. Kernel uses indirect tricks to access the high memory region. Data cache can go in this memory region.
  • LowTotal: The total amount of non-highmem memory.
  • LowFree: The amount of free memory of the low memory region. This is the memory the kernel can address directly. All kernel datastructures need to go into low memory.
  • SwapTotal: Total amount of physical swap memory.
  • SwapFree: Total amount of swap memory free.
  • Committed_AS: An estimate of how much RAM you would need to make a 99.99% guarantee that there never is OOM (out of memory) for this workload. Normally the kernel will overcommit memory. That means, say you do a 1GB malloc, nothing happens,
    really. Only when you start USING that malloc memory you will get real memory on demand, and just as much as you use. So you sort of take a mortgage and hope the bank doesn't go bust. Other cases might include when you mmap a file that's shared only when you
    write to it and you get a private copy of that data. While it normally is shared between processes. The Committed_AS is a guesstimate of how much RAM/swap you would need worst-case.

抱歉!评论已关闭.