是否可以监视Android中每种方法的CPU,内存使用情况

时间:2015-10-15 09:20:13

标签: android

我的Android应用程序有很多方法,也许每个方法运行1000行。我需要在每个方法的持续时间(ms)内监视CPU,内存使用情况,以便我可以知道哪个方法占用了大量的CPU,内存,我可以在以后对其进行优化。这可能吗?任何解决方案/建议都会有很大帮助:))

3 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

您可以设置2个DateTime变量,一个在开头,一个在结尾。

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

//Your method

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

从开始分散开始,看看需要多长时间。

记录结果:Log.I(“METHOD_NAME”,“持续时间”);

答案 2 :(得分:0)

考虑使用Traceview进行性能分析。

这可以帮助您跟踪每个线程/方法的时间线等。请看下面的链接。

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