有时我们需要获取手机的运行内存,可采用下面方法进行获取,返回的是内存的字节数。

    public static long getMemory(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
            if (manager != null) {
                manager.getMemoryInfo(info);
                return info.totalMem;
            }
        } else {
            try {
                FileReader localFileReader = new FileReader("/proc/meminfo");
                BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
                String load = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小
                Pattern p = Pattern.compile("(\\d+)");
                Matcher m = p.matcher(load);
                String value = "";
                while (m.find()) {
                    value = m.group(1);
                }
                long totalMem = Long.valueOf(value);
                return totalMem * 1024;
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
        return 0;
    }

也可使用adb命令进行查看

adb shell
cat /proc/meminfo

null