120 likes | 211 Views
Tracking tasks. GET_TASK. Make a new app, WatchProcesses Add permission GET_TASK This permission allows the app to collect lots of information about other tasks and kill apps. Collecting memory usage. In onCreate, add Context context = this.getApplicationContext();
E N D
GET_TASK • Make a new app, WatchProcesses • Add permission GET_TASK • This permission allows the app to collect lots of information about other tasks and kill apps
Collecting memory usage • In onCreate, add • Context context = this.getApplicationContext(); • ActivityManager mgr = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE); • List<RunningAppProcessInfo> processes = mgr.getRunningAppProcesses(); • Log.e("DEBUG", "Running processes:"); • for(Iteratori = processes.iterator(); i.hasNext(); ) • { • RunningAppProcessInfo p = (RunningAppProcessInfo)i.next(); • Log.e("DEBUG", " process name: "+p.processName); • Log.e("DEBUG", " pid: "+p.pid); • int[] pids = new int[1]; • pids[0] = p.pid; • android.os.Debug.MemoryInfo[] MI = mgr.getProcessMemoryInfo(pids); • Log.e("memory"," dalvik private: " + MI[0].dalvikPrivateDirty); • Log.e("memory"," dalvik shared: " + MI[0].dalvikSharedDirty); • Log.e("memory"," dalvik pss: " + MI[0].dalvikPss); • Log.e("memory"," native private: " + MI[0].nativePrivateDirty); • Log.e("memory"," native shared: " + MI[0].nativeSharedDirty); • Log.e("memory"," native pss: " + MI[0].nativePss); • Log.e("memory"," other private: " + MI[0].otherPrivateDirty); • Log.e("memory"," other shared: " + MI[0].otherSharedDirty); • Log.e("memory"," other pss: " + MI[0].otherPss); • Log.e("memory"," total private dirty memory (KB): " + MI[0].getTotalPrivateDirty()); • Log.e("memory"," total shared (KB): " + MI[0].getTotalSharedDirty()); • Log.e("memory"," total pss: " + MI[0].getTotalPss()); • } • run
Memory usage • In modern OS, app use shared libraries. • Hence, some memory is used by multiple apps, complicating determining an apps memory usage • dalvikPrivateDirty is the memory that would be freed by the java virtual machine if the process is killed • nativePrivateDirty is the same for native code • otherPrivateDirty is the same for some other code (not sure what else there is) • dalvikSharedDirty is the shared memory used by the java virtual machine • But this would not be freed if this app is killed • dalvikPss – an estimate of how much memory is used by the app. • This includes all the private memory, and a fraction of the shared memory • Check that pss >= private • The reason that only a fraction of shared memory is used is so that reasonability of the shared memory usage across all responsible apps • This value is used to estimate the memory load of the app and is used when considering whether to kill the app. • The totals are the sum over the dalvik, native, and other
Importance • In onCreate, add • Context context = this.getApplicationContext(); • ActivityManager mgr = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE); • List<RunningAppProcessInfo> processes = mgr.getRunningAppProcesses(); • Log.e("DEBUG", "importance:"); • for(Iteratori = processes.iterator(); i.hasNext(); ) • { • RunningAppProcessInfo p = (RunningAppProcessInfo)i.next(); • Log.e("DEBUG", " process name: "+p.processName); • Log.e("DEBUG", " importance value: "+p.importance); • switch (p.importance) { • case ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND: Log.e("DEBUG"," is IMPORTANCE_FOREGROUND"); break; • case ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE: Log.e("DEBUG"," is IMPORTANCE_VISIBLE"); break; • case ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE: Log.e("DEBUG"," is IMPORTANCE_SERVICE"); break; • case ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND: Log.e("DEBUG"," is IMPORTANCE_BACKGROUND"); break; • case ActivityManager.RunningAppProcessInfo.IMPORTANCE_EMPTY: Log.e("DEBUG"," is IMPORTANCE_EMPTY"); break; • default: Log.e("DEBUG"," should not happend"); break; • } • Log.e("DEBUG", " importance reason code: "+p.importanceReasonCode); • switch (p.importanceReasonCode) { • case ActivityManager.RunningAppProcessInfo.REASON_PROVIDER_IN_USE : Log.e("DEBUG"," is REASON_PROVIDER_IN_USE "); break; • case ActivityManager.RunningAppProcessInfo.REASON_SERVICE_IN_USE : Log.e("DEBUG"," is REASON_SERVICE_IN_USE "); break; • case ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN : Log.e("DEBUG"," is REASON_UNKNOWN "); break; • default : Log.e("DEBUG"," should not happen "); break; • } • Log.e("DEBUG", " fine grain importance: "+p.lru); • }
importance • When resources run out, Android will kill some processes. It uses importance to decide which can be killed • Five categories of importance • IMPORTANCE_BACKGROUND – expendable • IMPORTANCE_FOREGROUND – currently in the foreground • IMPORTANCE_PERCEPTIBLE - an app that the user is actively perceptible to the user. An example would be an application performing background music playback. (but not a service) • IMPORTANCE_SERVICE – a service that should remain running • IMPORTANCE_VISIBLE – is visible, but not in the foreground • IMPORTANCE_EMPTY - no actively running code • Three reasons for the importance, given by importanceReasonCode • REASON_PROVIDER_IN_USE • REASON_SERVICE_IN_USE • REASON_UNKNOWN • lru gives relative importance within a category • Only determined for IMPORTANCE_BACKGROUND category • Run with different apps in the background
Package list • Context context = this.getApplicationContext(); • ActivityManager mgr = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE); • List<RunningAppProcessInfo> processes = mgr.getRunningAppProcesses(); • Log.e("DEBUG", "packages:"); • for(Iteratori = processes.iterator(); i.hasNext(); ) • { • RunningAppProcessInfo p = (RunningAppProcessInfo)i.next(); • Log.e("DEBUG", " process name: "+p.processName); • Log.e("DEBUG", " user id: "+p.uid); // not sure what this is. It is not pid • for( String str : p.pkgList) • { • Log.e("DEBUG", " package: "+str); • } • }
Recent activities • In onCreate, add • Context context = this.getApplicationContext(); • ActivityManager mgr = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE); • List<ActivityManager.RecentTaskInfo> recentTasks = mgr.getRecentTasks(100,ActivityManager.RECENT_WITH_EXCLUDED ); • Log.e("DEBUG", "Recent tasks"); • for(Iteratori = recentTasks.iterator(); i.hasNext(); ) • { • RecentTaskInfo p = (RecentTaskInfo)i.next(); • Log.e("DEBUG", " origActivity: "+p.origActivity); • Log.e("DEBUG", " base intent action: "+p.baseIntent.getAction()); • Set<String> ss = p.baseIntent.getCategories(); • if (ss!=null) { • for (String a : ss) { • if (a!=null) • Log.e("DEBUG", " base intent category: "+a); • } • } • Log.e("DEBUG", " base intent comp name: "+p.baseIntent.getComponent().flattenToString()); • }
Recent activities • Does it show the same activity if the activity has repeatedly restarted? • What about if the activity was used intermittently? • Note that the intent gives a lot of information
Running services • In onCreate, add • Context context = this.getApplicationContext(); • ActivityManager mgr = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE); • List<ActivityManager.RecentTaskInfo> recentTasks = mgr.getRecentTasks(100,ActivityManager.RECENT_WITH_EXCLUDED ); • List<RunningServiceInfo> services = mgr.getRunningServices(100); • Log.e("DEBUG", "services:"); • for(Iterator<RunningServiceInfo> i = services.iterator(); i.hasNext(); ) • { • RunningServiceInfo p = (RunningServiceInfo)i.next(); • Log.e("DEBUG", " process name: "+p.process); • Log.e("DEBUG", " user id of owner: "+p.uid); • Log.e("DEBUG", " number of clients: "+p.clientCount); • Log.e("DEBUG", " client package name: "+p.clientPackage); • Log.e("DEBUG", " activeSince started (secs): "+p.activeSince/1000.0); • Log.e("DEBUG", " last active: "+p.lastActivityTime/1000.0); • }
killBackgroundProcesses • List<RunningServiceInfo> services = mgr.getRunningServices(100); • Log.e("DEBUG", "services:"); • for(Iterator<RunningServiceInfo> i = services.iterator(); i.hasNext(); ) • { • RunningServiceInfo p = (RunningServiceInfo)i.next(); • Log.e("DEBUG", " process name: "+p.process); • Log.e("DEBUG", " user id of owner: "+p.uid); • Log.e("DEBUG", " number of clients: "+p.clientCount); • Log.e("DEBUG", " client package name: "+p.clientPackage); • Log.e("DEBUG", " activeSince started (secs): "+p.activeSince/1000.0); • Log.e("DEBUG", " last active: "+p.lastActivityTime/1000.0); • } • Log.e("killing","killingcom.android.wallpaper"); • mgr.killBackgroundProcesses("com.android.wallpaper"); • services = mgr.getRunningServices(100); • Log.e("DEBUG", "services:"); • for(Iterator<RunningServiceInfo> i = services.iterator(); i.hasNext(); ) • { • RunningServiceInfo p = (RunningServiceInfo)i.next(); • Log.e("DEBUG", " process name: "+p.process); • Log.e("DEBUG", " user id of owner: "+p.uid); • Log.e("DEBUG", " number of clients: "+p.clientCount); • Log.e("DEBUG", " client package name: "+p.clientPackage); • Log.e("DEBUG", " activeSince started (secs): "+p.activeSince/1000.0); • Log.e("DEBUG", " last active: "+p.lastActivityTime/1000.0); • } • Restarts service.
Other things • getRunningTasks • But getRunningAppProcesses seems more useful • isUserAMonkey • Don’t know how to test