How to get memory usage and processor usage by application?

I found this code for general processor usage. can this be converted to indicate the use of the processor by the process? Is there any API with which we can use the processor and memory for Android?

private float readUsage() { try { RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r"); String load = reader.readLine(); String[] toks = load.split(" "); long idle1 = Long.parseLong(toks[5]); long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4]) + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]); try { Thread.sleep(360); } catch (Exception e) {} reader.seek(0); load = reader.readLine(); reader.close(); toks = load.split(" "); long idle2 = Long.parseLong(toks[5]); long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4]) + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]); return (float)(cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1)); } catch (IOException ex) { ex.printStackTrace(); } return 0; } 
+4
source share
4 answers
 Context context = this.getApplicationContext(); ActivityManager mgr = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE); List<RunningAppProcessInfo> processes = mgr.getRunningAppProcesses(); Log.e("DEBUG", "Running processes:"); for(Iterator i = 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()); } 
  • In modern operating systems, applications use shared libraries. Consequently, some memory is used by several applications, which complicates the determination of application memory usage.

  • dalvikPrivateDirty is the memory to be freed by java
    virtual machine if the process is killed

  • nativePrivateDirty is the same for native code, the same for some other code (not sure what else is)
  • otherPrivateDirty dalvikSharedDirty is the shared memory used by the java virtual machine But this will not be freed

  • if this application is killed by dalvikPss - an estimate of the amount of memory used by the application. This includes all personal memory and a fraction of the total memory. Make sure pss> = private. The reason is that only part of the shared memory is used, so the wisdom of using shared memory in all critical applications

This value is used to evaluate application memory usage.

Amounts represent the amount for dalwick, native and others.

+4
source

Reading / proc / [pid] / stat - [pid] - your target pid process

Then find the following members.

  • utime% lu
  • stime% lu

man / proc @ http://linux.die.net/man/5/proc

+2
source

Try the following:

  private float getCpuPer() { //for single process float cpuPer = 0; try { String[] cmd = {"top", "-n", "1"}; Process process = Runtime.getRuntime().exec(cmd); BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); // read the output from the command //System.out.println("Here is the standard output of the command:\n"); String s = null; while ((s = stdInput.readLine()) != null) { if (s.contains("your process name")) { String [] arr = s.split(" "); for (int i = 0; i < arr.length; i++) { if (arr[i].contains("%")) { s = arr[i].replace("%", ""); cpuPer = Float.parseFloat(s); break; } } //System.out.println(s); } } // read any errors from the attempted command //System.out.println("Here is the standard error of the command (if any):\n"); //while ((s = stdError.readLine()) != null) { //System.out.println(s); //} } catch (IOException e) { e.printStackTrace(); } return cpuPer; } 
0
source

When the application starts, run the DDMS tool, which is present in the Android-sdk \ tools folder. Where you get complete information about the entire running process. If you have an Android debugging device, you can even debug it from the device by connecting it to the system.

-1
source

Source: https://habr.com/ru/post/1402068/


All Articles