以编程方式获取android设备的所有RAM内存,而不仅仅是分配给用户进程的内存

我有一个设备,我知道它的RAM内存为512 MB

希望能够以编程方式检索此值(512 MB)。

到目前为止,我已经在互联网上主要涉及这两种方式:

https://stackoverflow.com/a/16143065/1521264这给了我386 MB

以及https://stackoverflow.com/a/23508821/1521264其中还提供了386 MB

我假设386 MB是用户进程可用的内存,所以我想得到所有内存或其他进程的细分。

我敢打赌你有与我相同的设备:谷歌Nexus S(或者它可能是具有类似配置的设备)。 这款手机拥有512 MB的物理RAM,但其中128个被GPU占用。 这就是为什么Android只有384 MB可用,这就是系统报告的数量。 来自维基百科条目 :

Nexus S拥有512 MB RAM(移动DDR)(128 MB分配给GPU,为操作系统留出384MB空闲)

由于这是系统可用的所有内存(而不仅仅是用户进程),因此您获得的值是正确的。

以下是通过以下代码查找已分配内存的方法:

{ 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)"); } 
  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 ); }