get Memory Info - Android Hardware

Android examples for Hardware:Memory

Description

get Memory Info

Demo Code


//package com.java2s;
import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;

import java.util.HashMap;

import android.content.Context;

public class Main {

    public static HashMap<String, Long> getMemoryInfo(Context context) {
        HashMap<String, Long> hmMeminfo = new HashMap<String, Long>();
        String meminfoPath = "/proc/meminfo";
        BufferedReader br = null;
        try {/*from  w ww  .  j  a v a 2  s  .c  om*/
            FileReader fr = new FileReader(meminfoPath);
            br = new BufferedReader(fr, 4096);
            String lineStr = null;
            while ((lineStr = br.readLine()) != null) {
                String[] lineItems = lineStr.split("\\s+");
                if (lineItems != null && lineItems.length == 3) {
                    String itemName = lineItems[0].substring(0,
                            lineItems[0].length() - 1);
                    long itemMemory = Long.valueOf(lineItems[1]);
                    hmMeminfo.put(itemName, itemMemory);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return hmMeminfo;
    }
}

Related Tutorials