Next: , Previous: How to create an instance of a Java class?, Up: FAQ - Frequently asked Questions


37.3.4 How can I handle memory limitations?

In order to execute Java code Octave creates a Java Virtual Machine (JVM). Such a JVM allocates a fixed amount of initial memory and may expand this pool up to a fixed maximum memory limit. The default values depend on the Java version (see javamem). The memory pool is shared by all Java objects running in the JVM. This strict memory limit is intended mainly to avoid that runaway applications inside web browsers or in enterprise servers can consume all memory and crash the system. When the maximum memory limit is hit, Java code will throw exceptions so that applications will fail or behave unexpectedly.

In Octave as well as in matlab, you can specify options for the creation of the JVM inside a file named java.opts. This is a text file where you can enter lines containing -X and -D options handed to the JVM during initialization.

In Octave, the Java options file must be located in the directory where javaclasspath.m resides, i.e., the package installation directory, usually something like ...\share\Octave\packages\java-1.2.8. You can find this directory by executing

     pkg list

In matlab, the options file goes into the MATLABROOT/bin/ARCH directory or in your personal matlab startup directory (can be determined by a ‘pwd’ command). MATLABROOT is the matlab root directory and ARCH is your system architecture, which you find by issuing the commands ‘matlabroot’ respectively ‘computer('arch')’.

The -X options allow you to increase the maximum amount of memory available to the JVM to 256 Megabytes by adding the following line to the java.opts file:

     -Xmx256m

The maximum possible amount of memory depends on your system. On a Windows system with 2 Gigabytes main memory you should be able to set this maximum to about 1 Gigabyte.

If your application requires a large amount of memory from the beginning, you can also specify the initial amount of memory allocated to the JVM. Adding the following line to the java.opts file starts the JVM with 64 Megabytes of initial memory:

     -Xms64m

For more details on the available -X options of your Java Virtual Machine issue the command ‘java -X’ at the operating system command prompt and consult the Java documentation.

The -D options can be used to define system properties which can then be used by Java classes inside Octave. System properties can be retrieved by using the getProperty() methods of the java.lang.System class. The following example line defines the property MyProperty and assigns it the string 12.34.

     -DMyProperty=12.34

The value of this property can then be retrieved as a string by a Java object or in Octave:

     octave> javaMethod('java.lang.System', 'getProperty', 'MyProperty');
     ans = 12.34

See also: javamem.