码迷,mamicode.com
首页 > 移动开发 > 详细

获取手机内存\可用内存\单个APP运行内存

时间:2014-09-25 12:27:58      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   color   io   os   ar   strong   for   

 1 /** 手机总内存 */
 2     private String getTotalMemory() {
 3         // 系统内存信息文件
 4         String str1 = "/proc/meminfo";
 5         String str2;
 6         String[] arrayOfString;
 7         long initial_memory = 0;
 8 
 9         try {
10             FileReader localFileReader = new FileReader(str1);
11             BufferedReader localBufferedReader = new BufferedReader(
12                     localFileReader, 8192);
13             // 读取meminfo第一行,系统总内存大小
14             str2 = localBufferedReader.readLine();
15 
16             arrayOfString = str2.split("\\s+");
17             for (String num : arrayOfString) {
18                 Log.i(str2, num + "\t");
19             }
20             // 获得系统总内存,单位是KB,乘以1024转换为Byte
21             initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;
22             localBufferedReader.close();
23         } catch (IOException e) {
24         }
25         // Byte转换为KB或者MB,内存大小规格化
26         return Formatter.formatFileSize(getBaseContext(), initial_memory);
27     }

 

1 /** 获取android当前可用内存大小 */ 
2     private String getAvailMemory() {
3         ActivityManager am = (ActivityManager) getSystemService(
Context.ACTIVITY_SERVICE);
4 MemoryInfo mi = new MemoryInfo(); 5 am.getMemoryInfo(mi); 6 // mi.availMem; 当前系统的可用内存 7 // 将获取的内存大小规格化 8 return Formatter.formatFileSize(getBaseContext(), mi.availMem); 9 }

 

 1 /** Android获取应用所占内存大小 */ 
 2 private void getRunningAppProcessInfo() {
 3   ActivityManager mActivityManager = (ActivityManager) getSystemService(
 4       Context.ACTIVITY_SERVICE);
 5 
 6    // 获得系统里正在运行的所有进程
 7    List<RunningAppProcessInfo> runningAppProcessesList = mActivityManager
 8         .getRunningAppProcesses();
 9 
10    for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcessesList) {
11       // 进程ID号
12       int pid = runningAppProcessInfo.pid;
13       // 用户ID
14       int uid = runningAppProcessInfo.uid;
15       // 进程名
16       String processName = runningAppProcessInfo.processName;
17       // 占用的内存
18       int[] pids = new int[] { pid };
19       Debug.MemoryInfo[] memoryInfo = mActivityManager
20              .getProcessMemoryInfo(pids);
21       int memorySize = memoryInfo[0].dalvikPrivateDirty;
22  
23       Log.i(TAG, "processName=" + processName + ",pid=" + pid + ",uid="
24             + uid + ",memorySize=" + memorySize + "kb");
25     }
26 }

 

 1 /**
 2   * 获取单个 app 内存限制大小
 3   * 返回以 M 为单位的数字,
 4   * 可能在不同的平台或者设备上值都不太一样
 5  */
 6  private void test01() {
 7    ActivityManager activityManager2 = (ActivityManager) getSystemService(
 8       Context.ACTIVITY_SERVICE);
 9    activityManager2.getMemoryClass();
10  }

 

获取手机内存\可用内存\单个APP运行内存

标签:android   style   blog   color   io   os   ar   strong   for   

原文地址:http://www.cnblogs.com/androidsj/p/3992320.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!