get Physical Memory K Bs - Android Phone

Android examples for Phone:Phone Information

Description

get Physical Memory K Bs

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    static long sPhysicalMemory = 0L;

    public static Long getPhysicalMemoryKBs() {
        // read /proc/meminfo to find MemTotal 'MemTotal: 711480 kB'
        // This operation would complete in fixed time

        if (sPhysicalMemory == 0L) {
            final String PATTERN = "MemTotal:";

            InputStream inStream = null;
            InputStreamReader inReader = null;
            BufferedReader inBuffer = null;

            try {
                inStream = new FileInputStream("/proc/meminfo");
                inReader = new InputStreamReader(inStream);
                inBuffer = new BufferedReader(inReader);

                String s;/*  www .  j a  v a2s.  c  om*/
                while ((s = inBuffer.readLine()) != null && s.length() > 0) {
                    if (s.startsWith(PATTERN)) {
                        String memKBs = s.substring(PATTERN.length())
                                .trim();
                        memKBs = memKBs.substring(0, memKBs.indexOf(' '));
                        sPhysicalMemory = Long.parseLong(memKBs);
                        break;
                    }
                }
            } catch (Exception e) {
            } finally {
                silentlyClose(inStream);
                silentlyClose(inReader);
                silentlyClose(inBuffer);
            }
        }

        return sPhysicalMemory;
    }

    public static void silentlyClose(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (Throwable e) {
            }
        }
    }
}

Related Tutorials