3 回答
TA贡献1804条经验 获得超2个赞
解析/proc/meminfo命令。您可以在这里找到参考代码: 在Android中获取内存使用情况 使用下面的代码并获取当前RAM: MemoryInfo mi = new MemoryInfo();ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); activityManager.getMemoryInfo(mi);double availableMegs = mi.availMem / 0x100000L;//Percentage can be calculated for API 16+double percentAvail = mi.availMem / (double)mi.totalMem * 100.0;
对0x100000L数的解释
1024 bytes == 1 Kibibyte 1024 Kibibyte == 1 Mebibyte1024 * 1024 == 10485761048576 == 0x100000
TA贡献1834条经验 获得超8个赞
final Runtime runtime = Runtime.getRuntime();final long usedMemInMB=(runtime.totalMemory() - runtime.freeMemory()) / 1048576L;final long maxHeapSizeInMB=runtime.maxMemory() / 1048576L;final long availHeapSizeInMB = maxHeapSizeInMB - usedMemInMB;
availHeapSizeInMB
val nativeHeapSize = Debug.getNativeHeapSize() val nativeHeapFreeSize = Debug.getNativeHeapFreeSize() val usedMemInBytes = nativeHeapSize - nativeHeapFreeSize val usedMemInPercentage = usedMemInBytes * 100 / nativeHeapSize
TA贡献1830条经验 获得超3个赞
public static long getUsedMemorySize() { long freeSize = 0L; long totalSize = 0L; long usedSize = -1L; try { Runtime info = Runtime.getRuntime(); freeSize = info.freeMemory(); totalSize = info.totalMemory(); usedSize = totalSize - freeSize; } catch (Exception e) { e.printStackTrace(); } return usedSize;}
- 3 回答
- 0 关注
- 1368 浏览
添加回答
举报