使用getRuntimeMemoryUsage()
获取内存使用情况Runtime.getRuntime()
但是当与探查器比较时找不到匹配项。
即分析器内存的java 显示22MB 但代码getRuntimeMemoryUsage()
返回12MB。
如何获取探查器中显示的内存使用情况?
fun getRuntimeMemoryUsage(): Long {
//calculate memory usage of currently running application:
var freeSize = 0L
var totalSize = 0L
var usedSize = 0L
try {
val runtime = Runtime.getRuntime()
runtime.gc()
freeSize = runtime.freeMemory() / 1048576L
totalSize = runtime.totalMemory() / 1048576L
usedSize = (totalSize - freeSize)
val maxHeapSizeInMB = runtime.maxMemory() / 1048576L
} catch (e: Exception) {
e.printStackTrace()
}
return usedSize
}