2011. 2. 21. 19:00

안드로이드 CPU Usage & Memory Usage 가져오기

안드로이드의 CPU 사용량과 메모리 사용량을 체크하는 방법입니다.

1. CPU 사용량

$ adb shell top -n 1

을 입력하면 CPU 사용량을 출력합니다.

top 에 대한 자세한 설명은 아래 명령을 입력합니다.

$ adb shell top --help

shell 명령을 Android App 에서 수행하려면 다음과 같이 합니다. ( shell 명령 수행시 App 의 권한은 항상 User 이기 때문에 su 권한을 필요로 하는 명령은 수행할 수 없음에 유의하셔야 합니다. )


                Runtime runtime = Runtime.getRuntime(); 
                Process process; 
                String res = "-0-"; 
                try { 
                        String cmd = "top -n 1"; 
                        process = runtime.exec(cmd); 
                        InputStream is = process.getInputStream(); 
                        InputStreamReader isr = new InputStreamReader(is); 
                        BufferedReader br = new BufferedReader(isr); 
                        String line ; 
                        while ((line = br.readLine()) != null) { 
                                String segs[] = line.trim().split("[ ]+"); 
                                if (segs[0].equalsIgnoreCase([Your Process ID])) { 
                                        res = segs[1]; 
                                        break; 
                                } 
                        } 
                } catch (Exception e) { 
                        e.fillInStackTrace(); 
                        Log.e("Process Manager", "Unable to execute top command"); 
                } 

2. 메모리 사용량

$ adb shell procrank

procrank 명령이 가장 정확한 메모리 사용 정보를 보여준다고 합니다. (by Dianne Hackborn)

특히, 
uss 칼럼은 app 고유의 메모리 사용량을 나타내고,
pss 는 또한 프로세스간에 공유되는 메모리 량을 포함하고 있습니다.

하지만 이 명령은 production device 에는 지원하지 않을수 있습니다. 그때는 다음의 명령을 사용합니다.

$ adb shell dumpsys meminfo
$ adb shell dumpsys meminfo [processname]

위의 명령을 들을 이용하여 CPU 와 Memory 의 사용량을 차트 등으로 한눈에 보여줄 수 있습니다.

참고:

아래 링크 추가