get Used Memory - Android android.os

Android examples for android.os:Memory

Description

get Used Memory

Demo Code

import java.util.List;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Debug;
import android.text.TextUtils;

public class Main {

  public static long getUsedMemory(Context context, String packageName) {
    if (context == null) {
      return -1;//from w  w w  .j a  v  a 2  s  .com
    }
    if (TextUtils.isEmpty(packageName)) {
      packageName = context.getPackageName();
    }
    long size = 0;
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> runapps = activityManager.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo runapp : runapps) {
      if (packageName.equals(runapp.processName)) {
        Debug.MemoryInfo[] processMemoryInfo = activityManager.getProcessMemoryInfo(new int[] { runapp.pid });
        size = processMemoryInfo[0].getTotalPrivateDirty() * 1024;
      }
    }
    return size;
  }
}

Related Tutorials