Android手機總內存和可用內存

參考: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.

發佈了26 篇原創文章 · 獲贊 0 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章