I want to find out the amount of memory available in the Java Virutal Machine. I tried:

<< Jlink`;InstallJava[];runtime = JavaNew["java.lang.Runtime"];runtime@freeMemory[]

but it throws an error:

JavaNew::argx0: "There is no constructor for class java.lang.Runtime that takes zero arguments."

However, the similar code:

dataObject = JavaNew["java.util.Date"];dataObject@toString[]

can print the result, although toString doesn't have arguments either.

What happens in Java? I noticed in the Java API that freeMemory is public native long freeMemory(). The difference is: toString is public String toString() and freeMemory is native. I found native's full name is Java Native Interface,but I don't have any idea to solve this. So how to use freeMemory() or totalMemory() etc. with JLink?

  • $\begingroup$ Just a warning about freememory (it's not really the total available amount), see here. $\endgroup$ Oct 23, 2014 at 13:34

1 Answer 1


This is how you can achieve what you try to do:

runtime = Runtime`getRuntime[]

I think the reason that you can not instanciate a java.lang.Runtime object the usual way is that it is a singleton and the only way to get a handle to the one instance is the Runtime`getRuntime class/static method (see e.g. oracle java docs). For that method to be available you need the call to LoadJavaClass.

  • $\begingroup$ Thank you for your answer! I think I know how to use the Runtime class now $\endgroup$
    – partida
    Oct 23, 2014 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.