Android 如何获取运行内存和总运行内存等

您所在的位置:网站首页 安卓系统运行内存占用多少 Android 如何获取运行内存和总运行内存等

Android 如何获取运行内存和总运行内存等

#Android 如何获取运行内存和总运行内存等| 来源: 网络整理| 查看: 265

目录 总存储内存和可用内存总运行内存和可用运行内存当前应用使用的运行内存大小获取应用的缓存大小

总存储内存和可用内存

1、获取手机总存储内存

/** * 当前的手机总存储内存大小 * * @return xx GB */ public String getTotalInternalMemorySize(Context context) { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return Formatter.formatFileSize(context, totalBlocks * blockSize); }

2、获取手机可用存储内存

/** * 当前手机可用存储内存大小 * * @return xx GB */ public String getAvailableInternalMemorySize(Context context) { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return Formatter.formatFileSize(context, availableBlocks * blockSize); } 总运行内存和可用运行内存

1、获取手机的总运行内存

/** * 获取android总运行内存大小 * @param context */ public String getTotalMemory(Context context) { String str1 = "/proc/meminfo";// 系统内存信息文件 String str2; String[] arrayOfString; long initial_memory = 0; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192); str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小 arrayOfString = str2.split("\\s+"); for (String num : arrayOfString) { Log.i(str2, num + "\t"); } // 获得系统总内存,单位是KB int i = Integer.valueOf(arrayOfString[1]).intValue(); //int值乘以1024转换为long类型 initial_memory = new Long((long) i * 1024); localBufferedReader.close(); } catch (IOException e) { } return Formatter.formatFileSize(context, initial_memory);// Byte转换为KB或者MB,内存大小规格化 }

2、获取手机可用运行内存

/** * 获取android当前可用运行内存大小 * @param context */ public String getAvailMemory(Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); am.getMemoryInfo(mi); // mi.availMem; 当前系统的可用内存 return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化 } 当前应用使用的运行内存大小 /** * 获取当前应用使用的内存大小 * * @return 单位 MB */ private double sampleMemory() { ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); double mem = 0.0D; try { final Debug.MemoryInfo[] memInfo = activityManager.getProcessMemoryInfo(new int[]{android.os.Process.myPid()}); if (memInfo.length > 0) { final int totalPss = memInfo[0].getTotalPss(); if (totalPss >= 0) { mem = totalPss / 1024.0D; } } } catch (Exception e) { e.printStackTrace(); } return mem; } 获取应用的缓存大小

1、获取应用缓存大小

/** * 获得缓存大小 * * @param context * @return * @throws Exception */ public String getTotalCacheSize(Context context) { long cacheSize = 0; try { cacheSize = getFolderSize(context.getCacheDir()); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { cacheSize += getFolderSize(context.getExternalCacheDir()); } } catch (Exception e) { e.printStackTrace(); } return getFormatSize(cacheSize); } private long getFolderSize(File file) throws Exception { long size = 0; try { File[] fileList = file.listFiles(); for (int i = 0; i < fileList.length; i++) { // 如果下面还有文件 if (fileList[i].isDirectory()) { size = size + getFolderSize(fileList[i]); } else { size = size + fileList[i].length(); } } } catch (Exception e) { e.printStackTrace(); } return size; }

2、清除应用缓存

/** * 清除缓存 * * @param context */ public void clearAllCache(Context context) { deleteDir(context.getCacheDir()); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { deleteDir(context.getExternalCacheDir()); } } private boolean deleteDir(File dir) { if (dir != null && dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } return dir.delete(); }

就这些方法吧,记录下,以防下次需要又找不到了!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3