我试图找到Scala程序的内存使用情况,我甚至得到了结果。我指的是以下代码:
object Test {
def main(args: Array[String]): Unit = {
val mb = 1024*1024
val runtime = Runtime.getRuntime
println("\nMemory in MB")
println("** Used Memory: " + (runtime.totalMemory - runtime.freeMemory)/mb)
println("** Free Memory: " + runtime.freeMemory/mb)
println("** Total Memory: " + runtime.totalMemory/mb)
println("** Max Memory: " + runtime.maxMemory/mb)
}
}
输出如下:
Memory in MB -
** Used Memory: 2
** Free Memory: 28
** Total Memory: 31
** Max Memory: 228
但是,此代码使用2MB内存的结果有多准确?有没有更好的方法来做这样的基准测试?