using the getRuntimeMemoryUsage()
to get memory usage from Runtime.getRuntime()
but when compare with the profiler couldn't find the match.
i.e. the profiler memory's java shows 22MB
but the code getRuntimeMemoryUsage()
return 12MB.
How to get the memory usage as what is shown in the profiler?
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
}