How to show memory usage in a running Scala application

You can show used, free, total, and max memory (RAM) available in a Scala application just like you do in Java. The lines of code look like this:

// memory info
val mb = 1024*1024
val runtime = Runtime.getRuntime
logger.log("** Used Memory:  " + (runtime.totalMemory - runtime.freeMemory) / mb)
logger.log("** Free Memory:  " + runtime.freeMemory / mb)
logger.log("** Total Memory: " + runtime.totalMemory / mb)
logger.log("** Max Memory:   " + runtime.maxMemory / mb)

That code will print the used, free, total, and maximum available memory in MB.

This code is a Scala version of the Java code at this page on viralpatel.net.

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.