How to retrieve the available RAM on your android device with Java

How to retrieve the available RAM on your android device with Java

The RAM on your Android device doesn't store only the linux kernel but other kind of data from all the installed applications, the GPU etc. The available RAM listed on the settings may not be the same as the total amount of RAM installed on your device. If you want to display this information on your android application, you will need to mess up with different code according to the version of Android that you are targeting.

In this article, we'll show you how to retrieve the devices's max available RAM size with Java in your Android Device.

A. Android API level >= 16 (JellyBean and above)

If your application only aims devices with the Android API Level >= 16, then you can use the method that relies on the Activity Manager of Android:

A.1. Get RAM size in Bytes

The following method returns the max available size of RAM in bytes. It returns a long variable e.g 1567342592:

import android.content.Context;
import java.text.DecimalFormat;
import android.app.ActivityManager;

/**
* Returns the available ammount of RAM of your Android device in Bytes e.g 1567342592 (1.5GB)
* @return {Long}
*/
public long getMemorySizeInBytes()
{
    Context context = getApplicationContext();

    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();

    activityManager.getMemoryInfo(memoryInfo);

    long totalMemory = memoryInfo.totalMem;

    return totalMemory;
}

A.2. Get RAM size in human readable format (Mb,Gb,Tb)

The following method returns the available size of RAM in a human readable format. It will return a string e.g 2GB, 1.5GB:

import android.content.Context;
import java.text.DecimalFormat;
import android.app.ActivityManager;

/**
* Returns the available ammount of RAM of your Android device in a human readable format e.g 1.56GB, 4GB, 512MB
*
* @return {String}
*/
public String getMemorySizeHumanized()
{
    Context context = getApplicationContext();

    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();

    activityManager.getMemoryInfo(memoryInfo);

    DecimalFormat twoDecimalForm = new DecimalFormat("#.##");

    String finalValue = "";
    long totalMemory = memoryInfo.totalMem;

    double kb = totalMemory / 1024.0;
    double mb = totalMemory / 1048576.0;
    double gb = totalMemory / 1073741824.0;
    double tb = totalMemory / 1099511627776.0;

    if (tb > 1) {
        finalValue = twoDecimalForm.format(tb).concat(" TB");
    } else if (gb > 1) {
        finalValue = twoDecimalForm.format(gb).concat(" GB");
    } else if (mb > 1) {
        finalValue = twoDecimalForm.format(mb).concat(" MB");
    }else if(kb > 1){
        finalValue = twoDecimalForm.format(mb).concat(" KB");
    } else {
        finalValue = twoDecimalForm.format(totalMemory).concat(" Bytes");
    }

    return finalValue;
}

B. Any Android API level

If your code needs to run on any Android version, you can (as in every linux based system) retrieve the amount of RAM through the /proc/meminfo file. The /proc/meminfo is used by the system to report the amount of free and used memory (both physical and swap) on the system as well as the shared memory and buffers used by the kernel.

In order to read the mentioned file on Android, rely on the RandomAccessFile class of Java to read files. The file contains a structure similar to:

Linux /proc/meminfo output example

So you will need to process the text to extract what you need, in our case we will do it with regular expression and keep it simple, the following method getMemorySizeHumanized returns a string with the human readable size available of RAM e.g 2GB:

import android.content.Context;
import java.io.RandomAccessFile;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.IOException;

/**
 * Returns the available amount of RAM of your Android device in a human readable format e.g 1.56GB, 4GB, 512MB
 *
 * @return {String}
 */
public String getMemorySizeHumanized() {
    RandomAccessFile reader;
    String load;
    DecimalFormat twoDecimalForm = new DecimalFormat("#.##");
    double totRam;
    String lastValue = "";

    try {
        reader = new RandomAccessFile("/proc/meminfo", "r");
        load = reader.readLine();
        Pattern p = Pattern.compile("(\\d+)");
        Matcher m = p.matcher(load);
        String value = "";
        while (m.find()) {
            value = m.group(1);
        }

        reader.close();

        totRam = Double.parseDouble(value);

        double mb = totRam / 1024.0;
        double gb = totRam / 1048576.0;
        double tb = totRam / 1073741824.0;

        if (tb > 1) {
            lastValue = twoDecimalForm.format(tb).concat(" TB");
        } else if (gb > 1) {
            lastValue = twoDecimalForm.format(gb).concat(" GB");
        } else if (mb > 1) {
            lastValue = twoDecimalForm.format(mb).concat(" MB");
        } else {
            lastValue = twoDecimalForm.format(totRam).concat(" KB");
        }

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

    return lastValue;
}

Happy coding !

This could interest you

Become a more social person