How does MATLAB use Java, and can I save memory by running without Java?

33 views (last 30 days)
I receive an "Out of memory" error when allocating a large array in MATLAB, and one of my colleagues suggested running MATLAB without using Java.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 Nov 2018
Java in MATLAB has several different uses, so the benefits depend on what you're trying to do. Java is used for at least 3 distinct reasons:
-- To implement IDE (Integrated Desktop Environment) functionality for folks who want a more modern visual interface for their MATLAB code editing, debugging, workspace navigation and inspection, profiling, user interface creation, etc.
-- To support calling Java code that users or developers write directly from MATLAB.
-- To somewhat simplify the issues involved with deploying the product on several different operating system and platforms.
As to the first use (the IDE), there's a thread on the comp.soft-sys.matlab newsgroup describing the pros and cons of using "matlab" vs using "matlab -nojvm". There are two good points made in the thread:
a) It basically depends on the kind of work your doing. Individual users may sometimes want to run with java and at other times without java.
b) There's now enough different widgets and tools in the IDE that it takes a while to explore all of the possibilities and fit them to how you work.
The second use (calling Java directly from A MATLAB file) has proven extremely popular and many recent MATLAB toolboxes are written at least partially in Java.
The questions above seem to imply that java's "heap memory system" is at the root of the original memory problems, and that MATLAB runs "on java" when the JVM mode is on. There is a Related Solution listed below which deals with the JVM's heap.
However, unless the original "Out of memory" error occurred while using Java objects, this does not seem to apply.
There is no question that using java involves significant additional memory. Assuming you are running on a Windows machine, the JVM itself, MATLAB's IDE, and the java heap all use some of the win32 address space, therefore less of that address space is available for MATLAB objects.
Each win32 process (such as MATLAB) starts with about 2 gig of memory into which it must fit its code, stack, and data. (The reason why a 32 bit machine doesn't give each process 4 gig is because Windows itself uses about half the space). If you run MATLAB with java, it takes up more of that 2 gig than if you run MATLAB without java. The main memory effect of this is to reduce the size of the largest array you could allocate, or the number of smaller arrays you could allocate. But MATLAB basic processing still runs the same way, and objects such as arrays are still allocated from the remainder of that 2 gig, NOT from the JVM heap, which is exclusively for java objects.
For more information about how processes in general, and MATLAB in particular, are mapped into memory, please see the following:

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!