MemInfoReader

来源:互联网 发布:ubuntu有哪些好的软件 编辑:程序博客网 时间:2024/05/22 13:45
在平时的开发中有时需要知道系统当前的总的memory是多少,那要如何获得呢?如下所示
先new 一个MemInfoReader,然后call readMemInfo()函数,这个时候就可以通过getTotalSize()方法获得.
200        MemInfoReader minfo = new MemInfoReader();
201        minfo.readMemInfo();
202        mTotalMemMb = minfo.getTotalSize()/(1024*1024);
readMemInfo的实现如下:
public final class MemInfoReader {
23    final long[] mInfos = new long[Debug.MEMINFO_COUNT];
24
25    public void readMemInfo() {
26        // Permit disk reads here, as /proc/meminfo isn't really "on
27        // disk" and should be fast.  TODO: make BlockGuard ignore
28        // /proc/ and /sys/ files perhaps?
29        StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
30        try {
31            Debug.getMemInfo(mInfos);
32        } finally {
33            StrictMode.setThreadPolicy(savedPolicy);
34        }
35    }
36
37    /**
38     * Total amount of RAM available to the kernel.
39     */
40    public long getTotalSize() {
41        return mInfos[Debug.MEMINFO_TOTAL] * 1024;
42    }
可见memory信息会存到mInfos 这个数组中 而debug.getMemInfo是一个native函数
public static native void getMemInfo(long[] outSizes);


其实现如下:
core/jni/android_os_Debug.cpp
static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out)
{
    char buffer[1024];
    int numFound = 0;
    if (out == NULL) {
        jniThrowNullPointerException(env, "out == null");
        return;
    }
    int fd = open("/proc/meminfo", O_RDONLY);
    if (fd < 0) {
        ALOGW("Unable to open /proc/meminfo: %s\n", strerror(errno));
        return;
    }
    int len = read(fd, buffer, sizeof(buffer)-1);
    close(fd);
    if (len < 0) {
        ALOGW("Empty /proc/meminfo");
        return;
    }
    buffer[len] = 0;
    static const char* const tags[] = {
            "MemTotal:",
            "MemFree:",
            "Buffers:",
            "Cached:",
            "Shmem:",
            "Slab:",
            "SwapTotal:",
            "SwapFree:",
            NULL
    };
    static const int tagsLen[] = {
            9,
            8,
            8,
            7,
            6,
            5,
            10,
            9,
            0
    };
    long mem[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    char* p = buffer;
    while (*p && numFound < 8) {
        int i = 0;
        while (tags[i]) {
            if (strncmp(p, tags[i], tagsLen[i]) == 0) {
                p += tagsLen[i];
                while (*p == ' ') p++;
                char* num = p;
                while (*p >= '0' && *p <= '9') p++;
                if (*p != 0) {
                    *p = 0;
                    p++;
                }
                mem[i] = atoll(num);
                numFound++;
                break;
            }
            i++;
        }
        while (*p && *p != '\n') {
            p++;
        }
        if (*p) p++;
    }
    fd = open("/sys/block/zram0/mem_used_total", O_RDONLY);
    if (fd >= 0) {
        len = read(fd, buffer, sizeof(buffer)-1);
        close(fd);
        if (len > 0) {
            buffer[len] = 0;
            mem[MEMINFO_ZRAM_TOTAL] = atoll(buffer)/1024;
        }
    }
    int maxNum = env->GetArrayLength(out);
    if (maxNum > MEMINFO_COUNT) {
        maxNum = MEMINFO_COUNT;
    }
    jlong* outArray = env->GetLongArrayElements(out, 0);
    if (outArray != NULL) {
        for (int i=0; i<maxNum; i++) {
            outArray[i] = mem[i];
        }
    }
    env->ReleaseLongArrayElements(out, outArray, 0);
}


可以是通过读取/proc/meminfo 来获得memory信息的.
0 0
原创粉丝点击