2012-09-18

Get current/total memory usage of Android

RandomAccessFile reader = null;
try {
    reader = new RandomAccessFile("/proc/meminfo", "r");
    long[] mems = new long[4];
    for(int i=0; i<4; i++)
    {
        String load = reader.readLine();
        String[] toks = load.split(":");
        mems[i] = Long.parseLong(toks[1].replace("kB", "").trim());
    }
    long total = mems[0]/1024;
    long free = (mems[1]+mems[2]+mems[3])/1024;
    int rate = (int)((float)(total-free)/total*100);
    Logger.d(TAG, "total "+total+" free "+free+" rate "+rate);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (reader!=null)
    {
        try {
                reader.close();
        } catch (IOException e) {
                e.printStackTrace();
        }
    }
}

No comments: