6

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?

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
partida
  • 6,816
  • 22
  • 48

1 Answers1

5

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.

Albert Retey
  • 23,585
  • 60
  • 104