Thursday, April 21, 2011

Mathematica & J/Link: Memory Constraints?

I am doing a computing-intensive benchmark using Mathematica and its J/Link Java interface.

The benchmark grinds to a halt if a memory footprint of about 320 MB is reached, since this seems to be the limit and the garbage collector needs more and more time and will eventually fail.

The Mathematica function ReinstallJava takes the argument command line. I tried to do

ReinstallJava[CommandLine -> "java -Xmx2000m ..."]

but Mathematica seems to ignore the -Xmx option completely.

How can I set the -Xmx memory option for my java program? Where does the limit of 320 MB come from? Any help would be greatly appreciated.

From stackoverflow
  • ReinstallJava takes a JVMArguments option. You can use it to pass heap size like so:

    In[1]:= Needs["JLink`"]
    
    In[2]:= Options[ReinstallJava]
    
    Out[2]= {ClassPath -> Automatic, CommandLine -> Automatic, 
     JVMArguments -> None, ForceLaunch -> False, Default -> Automatic, 
     CreateExtraLinks -> Automatic, "Asynchronous" -> Automatic}
    
    In[3]:= ?JVMArguments
    
    JVMArguments is an option to InstallJava that
    allows you to specify additional command-line
    arguments passed to the Java virtual machine at
    startup. The string you specify is added to the
    command line used to launch Java. You can use this
    option to specify properties with the standard -D
    syntax, such as "-Dsome.property=true". This
    option is not supported on Mac OSX. >>
    
    In[4]:= LoadJavaClass["java.lang.Runtime"];
    
    In[5]:= java`lang`Runtime`getRuntime[]@maxMemory[]
    
    Out[5]= 238616576
    
    In[6]:= ReinstallJava[JVMArguments -> "-Xmx64g"];
    
    In[7]:= LoadJavaClass["java.lang.Runtime"];
    
    In[8]:= java`lang`Runtime`getRuntime[]@maxMemory[]
    
    Out[8]= 61084008448
    

    (I once figured this out in desperation by reading through the code in C:\Program Files\Wolfram Research\Mathematica\7.0\SystemFiles\Links\JLink\Kernel. After noticing it was listed in Options[ReinstallJava] it seemed kind of obvious…)

0 comments:

Post a Comment