1

I have my Android app with lots of methods and maybe each method runs for 1000s of lines. I need to monitor the CPU,memory usage in time duration(ms) of each method so that i can know which method is taking lot of CPU,memory and i can optimize it later. Is this possible? Any solution/advise would be of great help :)

3 Answers3

1
I finally found out how to do this - 

//Calculate function execution time
double start = System.currentTimeMillis();
double end = System.currentTimeMillis();
double t = (end - start);

//Calculate the amount of time that the current thread has spent executing code or waiting for certain types of I/O.
double start1 = Debug.threadCpuTimeNanos();
double end1 = Debug.threadCpuTimeNanos();
double t1 = (end1 - start1)/1000000;

//CPU Usage
double cpu = (t1/t) * 100;

//if t=0,cpu becomes infinity programmatically but it means cpu usage is 0.
0

You could set 2 DateTime variables, one at the start, one at the end.

Time start= new Time();
now.setToNow();

//Your method

Time end = new Time();
now.setToNow();

Distract the start from the start and see how long it takes.

Log the result: Log.I("METHOD_NAME", "duration");

Geert Berkers
  • 653
  • 7
  • 19
  • This is for checking method execution time but i needed to know cpu,memory utilization of each method. –  Oct 15 '15 at 09:33
  • Sorry. My fault... Had to read. Maybe use this? http://stackoverflow.com/questions/3118234/get-memory-usage-in-android/5560634#5560634 – Geert Berkers Oct 15 '15 at 09:35
0

Consider using Profiling with Traceview.

This can help you trace the timeline of each thread/method etc. Look at the link below.

http://developer.android.com/tools/debugging/debugging-tracing.html#traceviewLayout

Dilberted
  • 1,172
  • 10
  • 23
  • Does it also give me CPU, memory utilization of each method? –  Oct 15 '15 at 09:43
  • Well you would need to use couple of dev tools in studio to get the info you need such as https://developer.android.com/tools/performance/memory-monitor/index.html#WorkingWithMemoryMonitor and ddms – Dilberted Oct 15 '15 at 09:48
  • This link should give you some more idea about memory monitor, heap viewer and allocation tracker https://developer.android.com/tools/performance/comparison.html – Dilberted Oct 15 '15 at 09:52
  • I need to achieve this programmatically rather than using profiling or traceview. –  Oct 15 '15 at 12:59