6
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ Just a warning about freememory (it's not really the total available amount), see here. $\endgroup$ – Sjoerd C. de Vries Oct 23 '14 at 13:34
5
$\begingroup$

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

Needs["JLink`"]
InstallJava[]
LoadJavaClass["java.lang.Runtime"]
runtime = Runtime`getRuntime[]
runtime@freeMemory[]

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.

$\endgroup$
  • $\begingroup$ Thank you for your answer! I think I know how to use the Runtime class now $\endgroup$ – partida Oct 23 '14 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.