Memory usage of a Scala program
<p>I am trying to find the memory usage of a Scala program I am even getting the result. I am referring the following code:</p>

<pre><code>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)
    }
}
</code></pre>

<p>Output is as follows:</p>

<pre><code>Memory in MB - 
** Used Memory:  2
** Free Memory:  28
** Total Memory: 31
** Max Memory:   228
</code></pre>

<p>However, how accurate is the result that this code is using 2MB memory? Is there a better way to do such bench-marking?</p>