Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scala or Java, how to get how much RAM does application currently occupy?

NetBeans IDE has a taskbar indicaror, showing how much RAM is currently allocated and used by the running instance. How can I get this data in my own application written in Scala? If there's no special function for this in Scala, I could use Java one.

like image 275
Ivan Avatar asked Sep 05 '10 01:09

Ivan


2 Answers

private val runtime = Runtime.getRuntime()
import runtime.{ totalMemory, freeMemory, maxMemory }

 System.out.println("New session, \ 
    total memory = %s, max memory = %s, free memory = %s".format(
        totalMemory, maxMemory, freeMemory))

Just copied from http://harrah.github.com/browse/samples/compiler/scala/tools/nsc/CompileServer.scala.html

like image 178
miku Avatar answered Nov 09 '22 08:11

miku


You can either use the totalMemory and maxMemory methods from the java.lang.Runtime class, or make use of the MemoryMXBean.

like image 45
Michael Barker Avatar answered Nov 09 '22 07:11

Michael Barker