Android examples for Hardware:Memory
Uses the meminfo command to get the exact available RAM
/******************************************************************************* * Copyright (c) 2011 MadRobot.//from www .j a v a 2 s. co m * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * Contributors: * Elton Kent - initial API and implementation ******************************************************************************/ //package com.java2s; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; public class Main { private static final String MEM_INFO_FILE = "/proc/meminfo"; /** * Usses the <code>meminfo</code> command to get the exact available RAM * * @return Available RAM size. 0 if the command failed. * @throws IOException */ public static int getRam() throws IOException { String ram = null; BufferedReader in = null; in = new BufferedReader(new FileReader(MEM_INFO_FILE)); String str; while ((str = in.readLine()) != null) { if (str.startsWith("MemTotal:")) { ram = str; break; } } in.close(); if (ram != null && ram.length() > 0) { StringTokenizer token = new StringTokenizer(ram, ":"); while (token.hasMoreElements()) { ram = (String) token.nextElement(); } ram = ram.trim(); } Integer ramValue = null; try { ramValue = Integer.parseInt(ram.substring(0, ram.length() - 3)); } catch (Exception e) { return 0; } if (ramValue > 0) { return ramValue; } return 0; } }