How to get total RAM size of a device?

人走茶凉 提交于 2019-11-26 11:06:16

问题


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?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/7374246/how-to-get-total-ram-size-of-a-device

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!