get Total RAM Size - Android Hardware

Android examples for Hardware:Memory

Description

get Total RAM Size

Demo Code


//package com.java2s;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static float getTotalRAM() {
        RandomAccessFile reader = null;
        String load = null;/*  w  w w  .  j a  v a2 s.com*/
        DecimalFormat twoDecimalForm = new DecimalFormat("#.##");
        double totRam = 0;
        float lastValue = 0;
        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;

            float mb = (float) (totRam / 1024.0f);
            float gb = (float) (totRam / 1048576.0f);
            float tb = (float) (totRam / 1073741824.0f);

            lastValue = gb;
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            // Streams.close(reader);
        }

        return lastValue;
    }
}

Related Tutorials