George Lungu
George Lungu

Reputation: 125

How to get Total RAM memory of the Device?

When i run my app on LG G4 or Note 3 which has 3GB of RAM, it shows that the device has 2GB of ram. Can someone explain where is the problem?

    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();
    }

Upvotes: 1

Views: 3975

Answers (1)

otc
otc

Reputation: 754

It does not seem to be a clear API for that but I used a simple hack:

Setup a lookup table with some of the power of 2s (like first 15-20) and find the nearest value to the "OS memory" retrieved from the API.

Had a similar problem to yours: Get android device's all RAM memory programmatically and not only what is only allocated to user processes

Upvotes: 1

Related Questions