打印Android程序所占内存

项目中内存溢出,故关注程序内存使用情况

写了个打印内存使用情况的脚本:

 

  1. import java.io.BufferedReader;  
  2. import java.io.BufferedWriter;  
  3. import java.io.File;  
  4. import java.io.FileWriter;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.InputStreamReader;  
  8.   
  9. public class CdMemoryReader {  
  10.     BufferedWriter writer;  
  11.   
  12. /** 
  13.  *  
  14. * @Description: TODO 
  15. * @param @param args  args[0] : process name  args[1]: execute time (int) args[2]: sleep time  (long) 
  16. * @return void 
  17. * @throws @param args 
  18. * @date:2010/06/19 
  19. * author: 
  20.  */  
  21.     public static void main(String args[]) {  
  22.         final String brower= "brower";  
  23.         final String mms= "mms";  
  24.           
  25.         long sleeptime = 2000;  
  26.         int times  = 1000 ;  
  27.         String procName = heco ;  
  28.           
  29.        // CdMemoryReader cdMemoryReader  = new CdMemoryReader() ;  
  30.         System.out.println();  
  31.         switch (args.length){  
  32.             default :  
  33.                 System.out.println("the args more than 3 is useless !");  
  34.                 System.out.println();  
  35.             case 3 :  
  36.                 try{  
  37.                     long sleepT = Long.parseLong(args[2]);  
  38.                     sleeptime = sleepT < 1000 ? 1000 : sleepT ;  
  39.                 }catch(NumberFormatException e){  
  40.                     System.out.println("the args: " +args[2] + "is not a number .");  
  41.                     System.exit(0);  
  42.                 }  
  43.             case 2 :  
  44.                 try{  
  45.                     int exectime = Integer.parseInt(args[1]);  
  46.                     times = exectime <= 0 ? 1 : exectime ;  
  47.                 }catch(NumberFormatException e){  
  48.                     System.out.println("the args: " +args[1] + "is not a number .");  
  49.                     System.exit(0);  
  50.                 }  
  51.             case 1 :  
  52.                 String name = args[0];  
  53.                      //这里可以不做判断 直接使用传入的参数 我们是特定打印  
  54.                 if (name.contains(brower)) {  
  55.                     procName = brower;  
  56.                 }else if (name.contains(mms)){  
  57.                     procName = mms;  
  58.                 }else {  
  59.                     System.out.println("the process name is wrong !!");  
  60.                     System.exit(0);  
  61.                 }    
  62.             case 0 :  
  63.                 System.out.println("proce name: " + procName);  
  64.                 System.out.println();  
  65.                 System.out.println("execute times: " + times);  
  66.                 System.out.println();  
  67.                 System.out.println("sleep time: " + sleeptime);  
  68.                 System.out.println();  
  69.                 new CdMemoryReader().cdAccess(sleeptime,times,20,procName);  
  70.                 break ;  
  71.         }  
  72.           
  73.     }  
  74.   
  75.     private void cdAccess(long time, int times, int lines,String procName) {  
  76.   
  77.         Runtime rt = Runtime.getRuntime();  
  78.         StringBuffer buffer = new StringBuffer();  
  79.         String title  = " PID      Vss      Rss      Pss      Uss  cmdline" ;  
  80.         System.out.println(title);  
  81.         try {  
  82.             writer = new BufferedWriter(new FileWriter(new File("D:/log.txt")));  
  83.             writer.write(title + "/n");  
  84.         } catch (IOException e) {  
  85.             // TODO Auto-generated catch block  
  86.             e.printStackTrace();  
  87.         }  
  88.         int printTime = 1 ;  
  89.         for (int i = 0; i < times; i++) {  
  90.             try {  
  91.                 String cmd = "adb shell procrank";  
  92.                 buffer.delete(0, buffer.length());  
  93.                 Process proc = rt.exec(cmd);  
  94.                 InputStream stderr = proc.getInputStream();  
  95.                 InputStreamReader isr = new InputStreamReader(stderr);  
  96.                 BufferedReader br = new BufferedReader(isr);  
  97.   
  98.                 String line = null;  
  99.                 int j = 0;  
  100.                 while ((line = br.readLine()) != null) {  
  101.                     if (j < lines) {  
  102.                         if (line.contains(procName)){  
  103.                             System.out.println(line + "  PrintTime : " + printTime+++ "  execute time : " + (i+1));  
  104.                             buffer.append(line + "/n");  
  105.                             break ;  
  106.                         }  
  107.                     }  
  108.                     j++;  
  109.                 }  
  110. //              buffer.append("/n");  
  111. //              buffer.append("/n");  
  112.                 writer.write(buffer.toString());  
  113.                 writer.flush();  
  114. //              System.out.println("buffer.toString()" + buffer.toString());  
  115.                   
  116.             } catch (Throwable t) {  
  117.                 t.printStackTrace();  
  118.             }  
  119.             try {  
  120.                 Thread.sleep(time);  
  121.             } catch (InterruptedException ie) {  
  122.                 ie.printStackTrace();  
  123.             }  
  124.         }  
  125.         try {  
  126.             writer.close();  
  127.         } catch (IOException e) {  
  128.             // TODO Auto-generated catch block  
  129.             e.printStackTrace();  
  130.         }  
  131.     }  
  132. }  

 

 日志文件默认放在D盘

将uss 的数据获取 放在excel 中可以生成以下的图,更直观些

 

from:  http://blog.csdn.net/zhanghw0917/archive/2010/06/19/5680229.aspx

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要获取Android设备上GPU内存使用率,可以使用以下步骤: 1. 在Android Studio中打开您的应用程序项目。 2. 在项目中添加以下依赖项: ```xml dependencies { implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:support-annotations:28.0.0' implementation 'com.android.support:design:28.0.0' implementation 'com.android.support:support-v4:28.0.0' implementation 'com.android.support:cardview-v7:28.0.0' implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.android.support:multidex:1.0.3' implementation 'com.google.android.gms:play-services-ads:17.2.0' implementation 'com.google.android.gms:play-services-vision:17.0.2' implementation 'com.google.android.gms:play-services-location:16.0.0' implementation 'com.google.android.gms:play-services-maps:16.0.0' implementation 'com.google.android.gms:play-services-auth:16.0.1' implementation 'com.google.android.gms:play-services-identity:16.0.0' implementation 'com.google.android.gms:play-services-games:16.0.0' implementation 'com.google.android.gms:play-services-drive:16.0.0' implementation 'com.google.android.gms:play-services-plus:16.0.0' implementation 'com.google.android.gms:play-services-panorama:16.0.0' implementation 'com.google.android.gms:play-services-nearby:16.0.0' } ``` 3. 在应用程序中添加以下代码: ```java private double getGpuUsage() { Process process = null; double gpuUsage = 0.0d; try { process = Runtime.getRuntime().exec("dumpsys meminfo"); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()), 1024); String line; int index = -1; while ((line = reader.readLine()) != null) { if (line.contains("Graphics")) { index = line.indexOf(":"); gpuUsage = Double.parseDouble(line.substring(index + 1, line.length() - 1).trim()); break; } } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (process != null) { process.destroy(); } } return gpuUsage; } ``` 4. 调用此方法以获取GPU内存使用率: ```java double gpuUsage = getGpuUsage(); ``` 5. 您可以将此值打印到控制台中,也可以将其发送到您的服务器以进行分析和监控。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值