“/proc/meminfo”文件記錄了android手機的一些信息

下面先對”/proc/meminfo”文件裏列出的字段進行粗略解釋:

MemTotal: 所有可用RAM大小。

MemFree: LowFree與HighFree的總和,被系統留着未使用的內存。

Buffers: 用來給文件做緩衝大小。

Cached: 被高速緩衝存儲器(cache memory)用的內存的大小(等於diskcache minus SwapCache)。

SwapCached:被高速緩衝存儲器(cache memory)用的交換空間的大小。已經被交換出來的內存,仍然被存放在swapfile中,用來在需要的時候很快的被替換而不需要再次打開I/O端口。

Active: 在活躍使用中的緩衝或高速緩衝存儲器頁面文件的大小,除非非常必要,否則不會被移作他用。

Inactive: 在不經常使用中的緩衝或高速緩衝存儲器頁面文件的大小,可能被用於其他途徑。

SwapTotal: 交換空間的總大小。

SwapFree: 未被使用交換空間的大小。

Dirty: 等待被寫回到磁盤的內存大小。

Writeback: 正在被寫回到磁盤的內存大小。

AnonPages:未映射頁的內存大小。

Mapped: 設備和文件等映射的大小。

Slab: 內核數據結構緩存的大小,可以減少申請和釋放內存帶來的消耗。

SReclaimable:可收回Slab的大小。

SUnreclaim:不可收回Slab的大小(SUnreclaim+SReclaimable=Slab)。

PageTables:管理內存分頁頁面的索引表的大小。

NFS_Unstable:不穩定頁表的大小。

獲取方式:

1. adb shell
# cat /proc/meminfo

2.

 

MainActivity.class

package com.example.sz.readsystemmemory;

import android.app.ActivityManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;

import android.app.ActivityManager.MemoryInfo;
import android.text.format.Formatter;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
    TextView tv1 = null;
    TextView tv2 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv1 = findViewById(R.id.system_memory1);
        tv2 = findViewById(R.id.system_memory2);
        //第一種方式:
        tv1.setText("第一種方式:手機總內存: " + this.getTotalMemory() + ", " + "可用內存: "
                + this.getAvailMemory());

        //第二種方法:手用java的反射機制來獲取手機的內存的一些信息。
        show();
}


    private String getAvailMemory() {// 獲取android當前可用內存大小

        ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        MemoryInfo mi = new MemoryInfo();
        am.getMemoryInfo(mi);
        //mi.availMem; 當前系統的可用內存

        return Formatter.formatFileSize(getBaseContext(), mi.availMem);// 將獲取的內存大小規格化
    }

    private String getTotalMemory() {
        String str1 = "/proc/meminfo";// 系統內存信息文件
        String str2;
        String[] arrayOfString;
        long initial_memory = 0;

        try {
            FileReader localFileReader = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(
                    localFileReader, 8192);
            str2 = localBufferedReader.readLine();// 讀取meminfo第一行,系統總內存大小

            arrayOfString = str2.split("\\s+");
            for (String num : arrayOfString) {
                Log.i(str2, num + "\t");
            }

            initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 獲得系統總內存,單位是KB,乘以1024轉換爲Byte
            localBufferedReader.close();

        } catch (IOException e) {
        }
        return Formatter.formatFileSize(getBaseContext(), initial_memory);// Byte轉換爲KB或者MB,內存大小規格化
    }


    public void show(){
        Method _readProclines = null;
        try {
            Class procClass;
            procClass = Class.forName("android.os.Process");
            Class parameterTypes[]= new Class[] {String.class, String[].class, long[].class };
            _readProclines = procClass.getMethod("readProcLines", parameterTypes);
            Object arglist[] = new Object[3];
            final String[] mMemInfoFields = new String[] {"MemTotal:",
                    "MemFree:", "Buffers:", "Cached:"};
            long[] mMemInfoSizes = new long[mMemInfoFields.length];
            mMemInfoSizes[0] = 30;
            mMemInfoSizes[1] = -30;
            arglist[0] = new String("/proc/meminfo");
            arglist[1] = mMemInfoFields;
            arglist[2] = mMemInfoSizes;
            if(_readProclines!=null){
                _readProclines.invoke(null, arglist);
                for (int i=0; i<mMemInfoSizes.length; i++) {
                    Log.d("GetFreeMem", mMemInfoFields[i]+" : "+mMemInfoSizes[i]/1024);
                    tv2.setText("第二種方式:手機總內存: " + this.getTotalMemory() + ", " + "可用內存: "
                            + this.getAvailMemory());
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/system_memory1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

    <TextView
        android:layout_marginTop="50dp"
        android:id="@+id/system_memory2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />
</LinearLayout>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章