32

I want to get full RAM size of a device. memoryInfo.getTotalPss() returns 0. There is not function for get total RAM size in ActivityManager.MemoryInfo.

How to do this?

Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
BOOMik
  • 423
  • 1
  • 5
  • 13
  • 1
    `public static synchronized int readTotalRam() { int tm=1000; try { RandomAccessFile reader = new RandomAccessFile("/proc/meminfo", "r"); String load = reader.readLine(); String[] totrm = load.split(" kB"); String[] trm = totrm[0].split(" "); tm=Integer.parseInt(trm[trm.length-1]); tm=Math.round(tm/1024); } catch (IOException ex) { ex.printStackTrace(); } return tm; }` – BOOMik Sep 12 '11 at 19:22
  • 1
    I write function for this. ^^^ answer ^^^ – BOOMik Sep 12 '11 at 19:24

6 Answers6

49

As of API level 16 you can now use the totalMem property of the MemoryInfo class.

Like this:

ActivityManager actManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
actManager.getMemoryInfo(memInfo);
long totalMemory = memInfo.totalMem;

Api level 15 and lower still requires to use the unix command as shown in cweiske's answer.

Community
  • 1
  • 1
Leon Lucardie
  • 9,541
  • 4
  • 50
  • 70
24

Standard unix command: $ cat /proc/meminfo

Note that /proc/meminfo is a file. You don't actually have to run cat, you can simply read the file.

cweiske
  • 30,033
  • 14
  • 133
  • 194
  • 1
    I reed /proc/meminfo file and received ram size. – BOOMik Sep 10 '11 at 21:59
  • Rolled back to the original author's version of this answer. While Leon posts something that works in later API levels, that does not mean that this method no longer works. 3rd party edits should not be used to comment on an answer - that is the role of comments, or in this case, Leon's own answer. – Chris Stratton Jul 06 '13 at 15:04
  • Additionally, I'll add that from code running on the device, there is no reason to run a `cat` process - instead, simply read `/proc/meminfo` from your java or native code as if it were a text file. Cat would only be needed if you were working in a shell, or accessing something which required you to start a process under a different userid with a hacked `su`. – Chris Stratton Jul 06 '13 at 15:05
23

I can get the usable RAM memory in such a way

public String getTotalRAM() {

    RandomAccessFile reader = null;
    String load = null;
    DecimalFormat twoDecimalForm = new DecimalFormat("#.##");
    double totRam = 0;
    String lastValue = "";
    try {
        reader = new RandomAccessFile("/proc/meminfo", "r");
        load = reader.readLine();

        // Get the Number value from the string
        Pattern p = Pattern.compile("(\\d+)");
        Matcher m = p.matcher(load);
        String value = "";
        while (m.find()) {
            value = m.group(1);
            // System.out.println("Ram : " + value);
        }
        reader.close();

        totRam = Double.parseDouble(value);
        // totRam = totRam / 1024;

        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();
    } finally {
        // Streams.close(reader);
    }

    return lastValue;
}

Tested Upto Android 4.3 : SAMSUNG S3

Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74
5

You can get the total RAM size by using this code:

var activityManager = GetSystemService(Activity.ActivityService) as ActivityManager;
var memoryInfo = new ActivityManager.MemoryInfo();
activityManager.GetMemoryInfo(memoryInfo);

var totalRam = memoryInfo.TotalMem / (1024 * 1024);

If the device has 1GB RAM, totalRam will be 1000.

Bruno A. Klein
  • 320
  • 3
  • 12
3

To get RAM values simply do:

ActivityManager actManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
    assert actManager != null;
    actManager.getMemoryInfo(memInfo);
    long totalMemory = memInfo.totalMem;
    long availMemory = memInfo.availMem;
    long usedMemory = totalMemory - availMemory;
    float precentlong = (((float) (availMemory / totalMemory)) * 100);

Here you will get Total as well as Free and Used RAM size. The value of these will be in "long", so format it to human-readable (i.e in MB/GB). Use the following method to do so:

 private String floatForm(double d) {
    return String.format(java.util.Locale.US, "%.2f", d);
}

private String bytesToHuman(long size) {
    long Kb = 1024;
    long Mb = Kb * 1024;
    long Gb = Mb * 1024;
    long Tb = Gb * 1024;
    long Pb = Tb * 1024;
    long Eb = Pb * 1024;

    if (size < Kb) return floatForm(size) + " byte";
    if (size >= Kb && size < Mb) return floatForm((double) size / Kb) + " KB";
    if (size >= Mb && size < Gb) return floatForm((double) size / Mb) + " MB";
    if (size >= Gb && size < Tb) return floatForm((double) size / Gb) + " GB";
    if (size >= Tb && size < Pb) return floatForm((double) size / Tb) + " TB";
    if (size >= Pb && size < Eb) return floatForm((double) size / Pb) + " Pb";
    if (size >= Eb) return floatForm((double) size / Eb) + " Eb";

    return "0";
}

So now for setting these value to any textview do:

totalRam_tv.setText("".concat(bytesToHuman(totalMemory)));
Akshay Nighot
  • 41
  • 1
  • 4
-1

Simple method to get the total and available RAM are given below:

//Method call returns the free RAM currently and returned value is in bytes.
Runtime.getRuntime().freeMemory();

//Method call returns the total RAM currently and returned value is in bytes.
Runtime.getRuntime().maxMemory();

Hope this will work.

For formatting the value to KB and MB, the following method can be used :

/**
     * Method to format the given long value in human readable value of memory.
     * i.e with suffix as KB and MB and comma separated digits.
     *
     * @param size Total size in long to be formatted. <b>Unit of input value is assumed as bytes.</b>
     * @return String the formatted value. e.g for input value 1024 it will return 1KB.
     * <p> For the values less than 1KB i.e. same input value will return back. e.g. for input 900 the return value will be 900.</p>
     */
    private String formatSize(long size) {
        String suffix = null;

        if (size >= 1024) {
            suffix = " KB";
            size /= 1024;
            if (size >= 1024) {
                suffix = " MB";
                size /= 1024;
            }
        }

        StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

        int commaOffset = resultBuffer.length() - 3;
        while (commaOffset > 0) {
            resultBuffer.insert(commaOffset, ',');
            commaOffset -= 3;
        }

        if (suffix != null) resultBuffer.append(suffix);
        return resultBuffer.toString();
    }

The method body can be customised to get desirable results.

Manmohan Soni
  • 6,472
  • 2
  • 23
  • 29
  • Runtime.getRuntime().maxMemory() returns 256MB on MEIZU X8. That is not correct. – Lywx Oct 10 '19 at 02:52
  • Have you tested the same code on some other devices? – Manmohan Soni Oct 18 '19 at 07:00
  • No, I assured that return only memory used by some part of Android Runtime, not total device memory. I switched to Klein's answer and found it working correctly. – Lywx Oct 21 '19 at 04:41