Java Code Examples for android.app.ActivityManager#getProcessMemoryInfo()

The following examples show how to use android.app.ActivityManager#getProcessMemoryInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MEMUtils.java    From GTTools with MIT License 6 votes vote down vote up
/**
 * 获取进程内存PSS数据
 *
 * @param context Android上下文
 * @param pid 进程ID
 * @return {nativePss,dalvikPss,TotalPss}
 */
public static long[] getPSS(Context context, int pid) {
	long[] value = new long[3]; // Natvie Dalvik Total
	if (pid >= 0) {
		int[] pids = new int[1];
		pids[0] = pid;
		ActivityManager mAm = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		MemoryInfo[] memoryInfoArray = mAm.getProcessMemoryInfo(pids);
		MemoryInfo pidMemoryInfo = memoryInfoArray[0];

		value[0] = pidMemoryInfo.nativePss;
		value[1] = pidMemoryInfo.dalvikPss;
		value[2] = pidMemoryInfo.getTotalPss();
	} else {
		value[0] = 0;
		value[1] = 0;
		value[2] = 0;
	}

	return value;
}
 
Example 2
Source File: GSAServiceClient.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
/**
 * Get the PSS used by the process hosting a service.
 *
 * @param packageName Package name of the service to search for.
 * @return the PSS in kB of the process hosting a service, or INVALID_PSS.
 */
@VisibleForTesting
static int getPssForService(ComponentName componentName) {
    if (componentName == null) return INVALID_PSS;
    Context context = ContextUtils.getApplicationContext();
    ActivityManager activityManager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> services =
            activityManager.getRunningServices(1000);
    if (services == null) return INVALID_PSS;
    int pid = -1;
    for (ActivityManager.RunningServiceInfo info : services) {
        if (componentName.equals(info.service)) {
            pid = info.pid;
            break;
        }
    }
    if (pid == -1) return INVALID_PSS;
    Debug.MemoryInfo infos[] = activityManager.getProcessMemoryInfo(new int[] {pid});
    if (infos == null || infos.length == 0) return INVALID_PSS;
    return infos[0].getTotalPss();
}
 
Example 3
Source File: BigTestUtils.java    From mytracks with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the memory usage of MyTracks process. This method would get the Pss
 * memory. Pss is the amount of memory shared with other processes, accounted
 * in a way that the amount is divided evenly between the processes that share
 * it. This is memory that would not be released if the process was
 * terminated, but is indicative of the amount that this process is
 * "contributing" to the overall memory load.
 * 
 * @param context
 */
public static String getMyTracksPssMemoryInfo(Context context) {
  int MyTracksProcessId = -1;
  ActivityManager activityManager = (ActivityManager) context
      .getSystemService(Activity.ACTIVITY_SERVICE);
  List<RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();

  for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
    if (runningAppProcessInfo.processName.equalsIgnoreCase(MYTRACKS_PROCESS_NAME)) {
      MyTracksProcessId = runningAppProcessInfo.pid;
      break;
    }
  }
  if (MyTracksProcessId == -1) {
    Assert.fail();
  }
  int pids[] = { MyTracksProcessId };
  android.os.Debug.MemoryInfo[] memoryInfoArray = activityManager.getProcessMemoryInfo(pids);
  int memoryUsage = memoryInfoArray[0].getTotalPss();
  return String.format(Locale.US, " MyTracks TotalPss Memory: %d (kB) ", memoryUsage);
}
 
Example 4
Source File: GSAServiceClient.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Get the PSS used by the process hosting a service.
 *
 * @param packageName Package name of the service to search for.
 * @return the PSS in kB of the process hosting a service, or INVALID_PSS.
 */
@VisibleForTesting
static int getPssForService(ComponentName componentName) {
    if (componentName == null) return INVALID_PSS;
    Context context = ContextUtils.getApplicationContext();
    ActivityManager activityManager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> services =
            activityManager.getRunningServices(1000);
    if (services == null) return INVALID_PSS;
    int pid = -1;
    for (ActivityManager.RunningServiceInfo info : services) {
        if (componentName.equals(info.service)) {
            pid = info.pid;
            break;
        }
    }
    if (pid == -1) return INVALID_PSS;
    Debug.MemoryInfo infos[] = activityManager.getProcessMemoryInfo(new int[] {pid});
    if (infos == null || infos.length == 0) return INVALID_PSS;
    return infos[0].getTotalPss();
}
 
Example 5
Source File: MEMUtils.java    From GTTools with MIT License 5 votes vote down vote up
/**
 * 获取进程内存Private Dirty数据
 * 
 * @param context Android上下文
 * @param pid 进程ID
 * @return {nativePrivateDirty,dalvikPrivateDirty,TotalPrivateDirty}
 */
public static long[] getPrivDirty(Context context, int pid) {

	ActivityManager mAm = (ActivityManager) context
			.getSystemService(Context.ACTIVITY_SERVICE);
	int[] pids = new int[1];
	pids[0] = pid;
	MemoryInfo[] memoryInfoArray = mAm.getProcessMemoryInfo(pids);
	MemoryInfo pidMemoryInfo = memoryInfoArray[0];
	long[] value = new long[3]; // Natvie Dalvik Total
	value[0] = pidMemoryInfo.nativePrivateDirty;
	value[1] = pidMemoryInfo.dalvikPrivateDirty;
	value[2] = pidMemoryInfo.getTotalPrivateDirty();
	return value;
}
 
Example 6
Source File: MemoryUtil.java    From MemoryMonitor with MIT License 5 votes vote down vote up
/**
 * 获取应用实际占用内存
 *
 * @param context
 * @param pid
 * @return 应用pss信息KB
 */
public static PssInfo getAppPssInfo(Context context, int pid) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    Debug.MemoryInfo memoryInfo = am.getProcessMemoryInfo(new int[]{pid})[0];
    PssInfo pssInfo = new PssInfo();
    pssInfo.totalPss = memoryInfo.getTotalPss();
    pssInfo.dalvikPss = memoryInfo.dalvikPss;
    pssInfo.nativePss = memoryInfo.nativePss;
    pssInfo.otherPss = memoryInfo.otherPss;
    return pssInfo;
}
 
Example 7
Source File: MemInfo.java    From AppCrawler with Apache License 2.0 5 votes vote down vote up
public static android.os.Debug.MemoryInfo getProcessMemInfo (int pid) {
    Context context = InstrumentationRegistry.getContext();
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    int[] pids = new int[1];
    pids[0] = pid;
    android.os.Debug.MemoryInfo[] mems = am.getProcessMemoryInfo(pids);
    return mems[0];
}
 
Example 8
Source File: MonitoringData.java    From android-speaker-audioanalysis with MIT License 5 votes vote down vote up
/**
 * Function computes "Total Private Dirty Memory" consumed by this application only.
 * @return
 */
public String getMemoryUsage()
{
	ActivityManager mgr = (ActivityManager)appContext.getSystemService("activity");
	String totalPrivateDirtyMemory = "";
	
	for(RunningAppProcessInfo processInfo : mgr.getRunningAppProcesses())
	{
		
        if(processInfo.processName.equals(SharedData.appProcessName)) {//appProcessName) ){
        	//Log.i(TAG, " MFCC pid: "+processInfo.pid);                    
		    int[] pids = new int[1];
		    pids[0] = processInfo.pid;
		    android.os.Debug.MemoryInfo[] MI = mgr.getProcessMemoryInfo(pids);
     	    Log.i(TAG,"MFCC total private dirty memory (KB): " + MI[0].getTotalPrivateDirty());
     	    
     	    
     	    totalPrivateDirtyMemory = String.valueOf(MI[0].getTotalPrivateDirty());
		    
		    break;
        }
    }
	
	return totalPrivateDirtyMemory;

	
}
 
Example 9
Source File: BindingActivity.java    From android-speaker-audioanalysis with MIT License 5 votes vote down vote up
/**
 * Function computes "Total Private Dirty Memory" consumed by this application only.
 * @return
 */
private String getMemoryUsage()
{
	Context context = getApplicationContext();
	ActivityManager mgr = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
	String totalPrivateDirtyMemory = "";
	
	for(RunningAppProcessInfo processInfo : mgr.getRunningAppProcesses())
	{
		
        if(processInfo.processName.equals(appProcessName) ){
        	//Log.i(TAG, " MFCC pid: "+processInfo.pid);                    
		    int[] pids = new int[1];
		    pids[0] = processInfo.pid;
		    android.os.Debug.MemoryInfo[] MI = mgr.getProcessMemoryInfo(pids);
     	    Log.i(TAG,"MFCC total private dirty memory (KB): " + MI[0].getTotalPrivateDirty());
     	    
     	    
     	    totalPrivateDirtyMemory = String.valueOf(MI[0].getTotalPrivateDirty());
		    
		    break;
        }
    }
	
	return totalPrivateDirtyMemory;

    
    
}
 
Example 10
Source File: ProcessManagerEngine.java    From MobileGuard with MIT License 4 votes vote down vote up
/**
 * get running processes info
 * you just can use it before Android 5.0
 * @param context
 * @return
 */
public static List<ProcessInfoBean> getRunningProcessesInfoCompat(Context context) {
    // get manager
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    PackageManager pm = context.getPackageManager();
    // get processes info
    List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses();
    // create list. Specific it init size
    List<ProcessInfoBean> infos = new ArrayList<>(processes.size());
    // get info
    for (ActivityManager.RunningAppProcessInfo processInfo : processes) {
        // create bean
        ProcessInfoBean bean = new ProcessInfoBean();
        // set value
        bean.setPackageName(processInfo.processName);
        // get package info
        ApplicationInfo applicationInfo = null;
        try {
            applicationInfo = pm.getApplicationInfo(bean.getPackageName(), PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            // if package is empty, continue
            continue;
        }
        // set icon
        bean.setIcon(applicationInfo.loadIcon(pm));
        // app name
        bean.setAppName(applicationInfo.loadLabel(pm).toString());
        // system app
        if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            bean.setSystemApp(true);
        }// if not, need set false. Actually it was.
        // memory
        Debug.MemoryInfo[] processMemoryInfo = am.getProcessMemoryInfo(new int[]{processInfo.pid});
        if (processMemoryInfo.length >= 1) {
            bean.setMemory(processMemoryInfo[0].getTotalPss() * 1024);
        }
        // add to list
        infos.add(bean);
    }

    return infos;
}
 
Example 11
Source File: ProcessManagerEngine.java    From MobileGuard with MIT License 4 votes vote down vote up
/**
 * get running processes info by proc
 * @param context
 * @return
 */
public static List<ProcessInfoBean> getRunningProcessesInfoByProc(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    PackageManager pm = context.getPackageManager();
    // get running app processes info
    List<AndroidAppProcess> processes = AndroidProcesses.getRunningAppProcesses();
    // create list. Specific it init size
    List<ProcessInfoBean> infos = new ArrayList<>(processes.size());
    for (AndroidAppProcess process: processes) {
        // create bean
        ProcessInfoBean bean = new ProcessInfoBean();
        // get package name
        bean.setPackageName(process.getPackageName());
        // check empty
        if(TextUtils.isEmpty(bean.getPackageName())) {
            continue;
        }
        // get package info
        ApplicationInfo applicationInfo = null;
        try {
            applicationInfo = pm.getApplicationInfo(bean.getPackageName(), PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            // if package is empty, continue
            continue;
        }
        // set icon
        bean.setIcon(applicationInfo.loadIcon(pm));
        // app name
        bean.setAppName(applicationInfo.loadLabel(pm).toString());
        // system app
        if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            bean.setSystemApp(true);
        }// if not, need set false. Actually it was.
        // memory
        Debug.MemoryInfo[] processMemoryInfo = am.getProcessMemoryInfo(new int[]{process.pid});
        if (processMemoryInfo.length >= 1) {
            bean.setMemory(processMemoryInfo[0].getTotalPss() * 1024);
        }
        // add to list
        infos.add(bean);
    }
    return infos;
}
 
Example 12
Source File: Memory.java    From batteryhub with Apache License 2.0 4 votes vote down vote up
/**
 * Read memory usage using the public Android API methods in ActivityManager,
 * such as MemoryInfo and getProcessMemoryInfo.
 *
 * @param context the Context from the running Activity.
 * @return array of int values with total and used memory, in KB.
 */
@TargetApi(21)
public static int[] readMemory(final Context context) {
    int[] pIds;
    int total;
    int used = 0;
    int counter = 0;
    int memoryUsed = 0;

    ActivityManager manager = (ActivityManager)
            context.getSystemService(Activity.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();

    manager.getMemoryInfo(memoryInfo);
    total = (int) memoryInfo.availMem;

    List<ActivityManager.RunningAppProcessInfo> processInfoList =
            manager.getRunningAppProcesses();
    List<ActivityManager.RunningServiceInfo> serviceInfoList =
            manager.getRunningServices(Integer.MAX_VALUE);

    if (processInfoList == null || serviceInfoList == null) {
        return new int[] {total, used};
    }

    pIds = new int[processInfoList.size() + serviceInfoList.size()];

    for (ActivityManager.RunningAppProcessInfo processInfo : processInfoList) {
        pIds[counter] = processInfo.pid;
        counter++;
    }

    for (ActivityManager.RunningServiceInfo serviceInfo : serviceInfoList) {
        pIds[counter] = serviceInfo.pid;
        counter++;
    }

    // Two approaches one for Debug and other for Production
    // TODO: Do further testing to check Debug.MemoryInfo vs ActivityManager.MemoryInfo...

    // 1) ActivityManager.MemoryInfo
    manager.getMemoryInfo(memoryInfo);

    String message =
            String.format(Locale.US, "%d availMem, %b lowMemory, %d threshold, %d total",
                    memoryInfo.availMem, memoryInfo.lowMemory,
                    memoryInfo.threshold, memoryInfo.totalMem);

    logI(TAG, message);

    // 2) Debug.MemoryInfo
    Debug.MemoryInfo[] memoryInfosArray = manager.getProcessMemoryInfo(pIds);

    for (Debug.MemoryInfo memory : memoryInfosArray) {
        memoryUsed += memory.getTotalPss();
    }

    return new int[] {total, used};
}