如何获得设备的总RAM大小?

时间:2011-09-10 19:54:22

标签: android size ram

我希望获得设备的完整RAM大小。 memoryInfo.getTotalPss()返回0. ActivityManager.MemoryInfo中没有获取总RAM大小的函数。

怎么做?

6 个答案:

答案 0 :(得分:34)

从API级别16开始,您现在可以使用totalMem类的MemoryInfo属性。

像这样:

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

Api等级15及更低版仍然需要使用unix命令,如cweiske's answer所示。

答案 1 :(得分:19)

标准unix命令:$ cat /proc/meminfo

请注意/proc/meminfo是一个文件。您实际上不必运行cat,您只需阅读该文件即可。

答案 2 :(得分:18)

我可以用这种方式获得可用的RAM内存

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

经测试Android 4.3:SAMSUNG S3

答案 3 :(得分:3)

您可以使用以下代码获取总RAM大小:

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

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

如果设备有1GB RAM,则totalRam将为1000。

答案 4 :(得分:1)

获得总内存和可用RAM的简单方法如下:

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

希望这会起作用。

要将值格式化为KB和MB,可以使用以下方法:

// parent render function
render() { 
  const funcReference = () => { console.log('click') };
  return (<my-component myFunction={funcReference}></my-component>);
}

可以对方法主体进行自定义以获得理想的结果。

答案 5 :(得分:0)

要获取 RAM 值,只需执行以下操作:

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

在这里您将获得总计以及可用和已用 RAM 大小。 这些的值将是“长”,因此将其格式化为人类可读的(即以 MB/GB 为单位)。 使用以下方法执行此操作:

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

现在将这些值设置为任何文本视图:

totalRam_tv.setText("".concat(bytesToHuman(totalMemory)));