如何获得设备的总RAM内存?

时间:2016-04-18 11:35:02

标签: android performance ram

当我在LG G4上运行我的应用程序或者有3GB内存的Note 3时,它显示该设备有2GB内存。有人可以解释问题出在哪里?

    ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    activityManager.getMemoryInfo(mi);
    long freeMemory = mi.availMem / 1048576L;
    long totalMemory = mi.totalMem / 1048576L;
    long usedMemory = totalMemory - freeMemory;
    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(Util.MyPREFERENCES, 0);
    String status = sharedPreferences.getString(Util.KEY_STATUS, null);
    if (status.equals("clean")) {
        tvFree.setText(freeMemory + " MB");
        tvTotal.setText(totalMemory + " MB");
        tvUsed.setText(usedMemory + " MB");
        stateImage.setImageResource(R.drawable.greenstate);
        rippleBackground.stopRippleAnimation();
    }

1 个答案:

答案 0 :(得分:1)

它似乎不是一个明确的API,但我使用了一个简单的黑客:

设置一个具有2s幂的查找表(如前15-20),并找到从API检索到的“OS内存”的最近值。

遇到类似的问题: Get android device's all RAM memory programmatically and not only what is only allocated to user processes