Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current free RAM available in android device [duplicate]

In a device when we go in Manage apps->running tab then at bottom we see the used memory & free memory live changing their value.

How can we code it to my app to display just the same statistics ?

Thanks

like image 914
Vivek Warde Avatar asked Oct 10 '14 08:10

Vivek Warde


2 Answers

ActivityManager.MemoryInfo

MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager = (ActivityManager)getActivity(). getSystemService(Context.ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
long availableMegs = mi.availMem / 1048576L;

long percentAvail = mi.availMem / mi.totalMem;
like image 190
Suvitruf - Andrei Apanasik Avatar answered Nov 02 '22 04:11

Suvitruf - Andrei Apanasik


You can get using this method.

private float readUsage() {
    try {
        RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
        String load = reader.readLine();

        String[] toks = load.split(" ");

        long idle1 = Long.parseLong(toks[4]);
        long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[5])
              + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);

        try {
            Thread.sleep(360);
        } catch (Exception e) {}

        reader.seek(0);
        load = reader.readLine();
        reader.close();

        toks = load.split(" ");

        long idle2 = Long.parseLong(toks[4]);
        long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[5])
            + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);

        return (float)(cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1));

    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return 0;
} 
like image 34
Suresh Parmar Avatar answered Nov 02 '22 05:11

Suresh Parmar