Get Memory Total : Memory « Hardware « Android

Home
Android
1.2D Graphics
2.Animation
3.Core Class
4.Database
5.Date Type
6.Development
7.File
8.Game
9.Hardware
10.Media
11.Network
12.Security
13.UI
14.User Event
Android » Hardware » Memory 
Get Memory Total
  
//package org.anddev.andengine.util;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Scanner;
import java.util.regex.MatchResult;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;

/**
 * (c) 2010 Nicolas Gramlich 
 * (c) 2011 Zynga Inc.
 
 @author Nicolas Gramlich
 @since 15:50:31 - 14.07.2010
 */
class SystemUtils {
  private static final String BOGOMIPS_PATTERN = "BogoMIPS[\\s]*:[\\s]*(\\d+\\.\\d+)[\\s]*\n";
  private static final String MEMTOTAL_PATTERN = "MemTotal[\\s]*:[\\s]*(\\d+)[\\s]*kB\n";
  private static final String MEMFREE_PATTERN = "MemFree[\\s]*:[\\s]*(\\d+)[\\s]*kB\n";


  /**
   @return in kiloBytes.
   @throws SystemUtilsException
   */
  public static int getMemoryTotal() throws Exception {
    final MatchResult matchResult = SystemUtils.matchSystemFile("/proc/meminfo", MEMTOTAL_PATTERN, 1000);

    try {
      if(matchResult.groupCount() 0) {
        return Integer.parseInt(matchResult.group(1));
      else {
        throw new Exception();
      }
    catch (final NumberFormatException e) {
      throw new Exception(e);
    }
  }

  private static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizonthrows Exception {
    InputStream in = null;
    try {
      final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start();

      in = process.getInputStream();
      final Scanner scanner = new Scanner(in);

      final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon!= null;
      if(matchFound) {
        return scanner.match();
      else {
        throw new Exception();
      }
    catch (final IOException e) {
      throw new Exception(e);
    
      
  }
}

   
    
  
Related examples in the same category
1.Get memory information
2.Get Memory Size Strings
3.Get Used Memory Size
4.Get Free Memory Size
5.Get Total Memory Size
6.Calculates the free memory of the device. This is based on an inspection of the filesystem, which in android devices is stored in RAM.
7.Calculates the total memory of the device. This is based on an inspection of the filesystem, which in android devices is stored in RAM.
8.Get Memory Free
9.get MemoryInfo
10.Memory information
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.