3

I have a device that I know for sure it has RAM memory of 512 MB

Want to be able to retrieve this value (512 MB) Programmatically.

So far I have ran into predominately into these two ways on the internet:

https://stackoverflow.com/a/16143065/1521264 Which gives me 386 MB

and also https://stackoverflow.com/a/23508821/1521264 Which also gives 386 MB

I am assuming 386 MB is the memory available to user processes so I would like to get all the memory or a breakdown of the other processes.

Community
  • 1
  • 1
otc
  • 694
  • 1
  • 9
  • 40
  • Your assumption is wrong. – iheanyi Oct 31 '14 at 21:52
  • @iheanyi enlighten us – otc Nov 03 '14 at 14:59
  • 1
    http://developer.android.com/reference/android/app/ActivityManager.MemoryInfo.html According to that, you can get the total memory accessible by the kernel and the available memory to the system. Therefore, any memory not reported is not available to use by anything user or kernel. – iheanyi Nov 03 '14 at 16:25
  • And I totally agree with that comment but my question is: how can I find 0ut through code what this not reported memory adds up to? – otc Nov 03 '14 at 17:25
  • The short answer - you cannot - at least through the android api (at this point in time). You might be able to write device native code that can query the memory controller directly and get that information, but you'd need to write that code on a device by device basis. (Also voted up your question because I see no reason for it to have been voted down). – iheanyi Nov 03 '14 at 19:57

3 Answers3

5

I bet you have the same device as I do: The Google Nexus S (or it might be a device with a similar configuration). This phone has 512 MB of physical RAM, but 128 of it is taken up by the GPU. That's why only 384 MB are available to Android, and that's the amount the system reports. From the Wikipedia entry:

The Nexus S has 512 MB of RAM (Mobile DDR) (128MB is assigned to the GPU, leaving 384MB free for the OS)

Since that's all the memory that's available to the system (not just the user processes), the value you get is correct.

Axel
  • 13,939
  • 5
  • 50
  • 79
  • Axel, I am trying to find out a way to programmatically obtain the total memory (512 or 1024 or whatever the device has) because it will be different in every device my program will be running. – otc Nov 03 '14 at 15:04
  • So is there any way to get through code the memory you labeled " (Mobile DDR)" – otc Nov 03 '14 at 17:24
  • 2
    I think there is no way because it's limited by the hardware. Your device has 512mb physically, but the hardware splits it up between gpu and memory available to the system. You could perhaps find out about the memory available to the gpu and add that to the value the system reports. But I don't know if there's a way to find out if the gpu memory is on the same physical chip. Of course if both add up to a power of 2 when either of them is no power of 2, that would be a strong hint because AFAIK, all RAM modules ship with a memory amount that is a power of 2. – Axel Nov 03 '14 at 17:43
  • This kind of solves my problem in a very intelligent way :). I will be doing a lookup table with all the power of 2 and find the nearest value to the "OS memory". In case no one will post a programmatic way of retrieving the exact value I will mark you as the correct answer. – otc Nov 03 '14 at 18:52
0

Here is the way to find out the allocated memory by following code:

{
        Double allocated = new Double(Debug.getNativeHeapAllocatedSize())/new Double((1048576));
        Double available = new Double(Debug.getNativeHeapSize())/1048576.0;
        Double free = new Double(Debug.getNativeHeapFreeSize())/1048576.0;
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);

        Log.d("tag", "debug.heap native: allocated " + df.format(allocated) + "MB of " + df.format(available) + "MB (" + df.format(free) + "MB free)");
        Log.d("tag", "debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory()/1048576)) + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory()/1048576))+ "MB (" + df.format(new Double(Runtime.getRuntime().freeMemory()/1048576)) +"MB free)");

    }
AnkitRox
  • 534
  • 1
  • 6
  • 16
0
  public void 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);
        }

        scanBtn.setText("" +"/"+lastValue );
    }