【问题标题】:How to increase CPU usage of particular application in android如何在android中增加特定应用程序的CPU使用率
【发布时间】:2012-10-19 14:10:03
【问题描述】:

在我的应用程序中,我想将应用程序的 CPU 使用率增加到用户的期望值并使其稳定直到用户感兴趣。
我正在使用 top 命令使用以下方法计算总 CPU 使用率。

示例代码:

private long getCpuUsageStatistic() {
           usage =0;
          String tempString = executeTop(); // executeTop is method where I fire top command.


           tempString = tempString.replaceAll(",", "");
           tempString = tempString.replaceAll("User", "");
           tempString = tempString.replaceAll("System", "");
           tempString = tempString.replaceAll("IOW", "");
           tempString = tempString.replaceAll("IRQ", "");
           tempString = tempString.replaceAll("%", "");
           for (int i = 0; i < 10; i++) {
                tempString = tempString.replaceAll("  ", " ");
           }
           tempString  = tempString.replaceAll("  ", " ");
           tempString = tempString.trim();
           String[] myString = tempString.split(" ");
           int[] cpuUsageAsInt = new int[myString.length]; 
           for (int i = 0; i < 2; i++) {
                myString[i] = myString[i].trim();
                cpuUsageAsInt[i] = Integer.parseInt(myString[i]);
                //System.out.println("getCpuUsageStatistic-----"+cpuUsageAsInt[i]);
           }
           usage = cpuUsageAsInt[0]+cpuUsageAsInt[1];
           System.out.println("getCpuUsageStatistic-----"+usage); 
           return usage;
     }


And trying to incrase CPU usage by reducing sleeping time of running thread.

but, application COU usage is not increased.

My run method is like following::

public void run() {


           while (true) {
                try {

                     Thread.sleep(READ_INTERVAL);
                     totalUsage += cpuUpdate();
                     count++;
                     float cpuUsage = totalUsage / count;

                     if (count >= 10) {
                           if (cpuUsage >= desiredCpuUsage) {
                                System.out.println("cpu usage::%" + cpuUsage);

                                break;
                           } else {


                                System.out.println("cpu usage::%" + cpuUsage);
                         if(READ_INTERVAL / reducingFactor < 190)
                         {
                             if(cpuUsage / desiredCpuUsage< 0.2)
                             {
                                 reducingFactor = reducingFactor / 1.2;
                             }
                             else if(cpuUsage / desiredCpuUsage < 0.5)
                             {
                                 reducingFactor = reducingFactor / 1.5;
                             }
                             else if(cpuUsage / desiredCpuUsage < 0.6)
                             {
                                 reducingFactor = reducingFactor / 3.0;
                             }
                             else if(cpuUsage / desiredCpuUsage < 0.7)
                             {
                                 reducingFactor = reducingFactor / 4.0;
                             }
                             else if(cpuUsage / desiredCpuUsage < 0.75)
                             {
                                 reducingFactor = reducingFactor / 5.0;
                             }
                             else if(cpuUsage / desiredCpuUsage < 0.8)
                             {
                                 reducingFactor = reducingFactor / 6.0;
                             }
                             else if(cpuUsage / desiredCpuUsage < 0.85)
                             {
                                 reducingFactor = reducingFactor / 7.0;
                             }
                             else if(cpuUsage / desiredCpuUsage < 0.9)
                             {
                                 reducingFactor = reducingFactor / 9.0;
                             }
                             else if(cpuUsage / desiredCpuUsage < 0.95)
                             {
                                 reducingFactor = reducingFactor / 12.0;
                             }
                             else
                             {
                                 reducingFactor = reducingFactor / 15.0;
                             }
                         }
                         READ_INTERVAL-= reducingFactor;
                     }
                           cpuUsage=0;
                           count=0;
                           System.out.println("Read_interval" + READ_INTERVAL
                                     + "::cpu usage::%::" + cpuUsage);

                 }          



                           }


                catch (Exception e) {
                     e.printStackTrace();
                }  
           }
     }



请提供解决方案和任何技术来增加或减少 cpu 的使用率。 提前致谢。

【问题讨论】:

  • 你正在使用一个空字符串对象!
  • 不,它不为空。 m 在该方法中解析 top 命令输出。
  • @NikkyD 感谢您的建议。我已经编辑了我的问题。

标签: android cpu-usage


【解决方案1】:

我建议在带有 Thread.sleep 的 while 循环中使用一些数学函数来限制它。

我不确定是否可以强制 Dalvik VM 接受负载,或者它是否会优化其运行时计划以平衡负载。

我只知道很多 sin/cos 函数会创建一个相当稳定的负载,理论上应该可以通过简单地添加更多或更少的计算来轻松扩展和缩减。

我只是不知道虚拟机如何在多核 cpu 上处理这个问题,因为如果它突然将负载转移到另一个核心上,第一个核心上的负载就会下降,或者更糟,一直在变化。

但我觉得值得一试

【讨论】:

  • 我正在使用相同的...但我没有得到稳定的负载。
  • 它的 java,你有垃圾收集和一个随时占用 CPU 资源的系统。你有百分之几?
  • 是的..它不是一直都一样。有时它显示 CPU 使用率计算增加了 10% 到 15%。但是,实际上,它并没有增加那么多。达到预期值后,其使用量最终会减少。
  • 虚拟机在运行时进行优化,所以一旦它看到你一遍又一遍地做同样的事情,它就会让这些事情变得更快,负载也会下降一点。
猜你喜欢
  • 2011-09-20
  • 1970-01-01
  • 2010-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
相关资源
最近更新 更多