Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: MATLAB calling Java methods (performance)
Date: Tue, 10 Mar 2009 22:23:01 +0000 (UTC)
Organization: University of Utah
Lines: 16
Message-ID: <gp6p85$sit$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1236723781 29277 172.30.248.35 (10 Mar 2009 22:23:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 10 Mar 2009 22:23:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 827053
Xref: news.mathworks.com comp.soft-sys.matlab:523891

Recently, I've written a Java component which loads a large amount of data from a database (55000 rows which is about 30Mbyte). After the load I had to do an insertion sort. I had the java call in a MATLAB tight loop essentially looping through the number of records brought back from the database:

%      waitnow = 0;
%      waitmax = numBuild;
%      i = 0;
%       while i<numBuild,
%          myJavaComponent.build(i);
%          i = i + 1;
%          waitnow = waitnow + i;
%          waitbar(waitnow/waitmax,wbHandle);
%       end;

While this works fine, the result was that the JVM garbage collector constantly performing minor garbage collections (indicated by logging gc activities in java.opts) and chew up 100% of the CPU. After using a profiler (integrated to Eclipse 3.2) to profile my code, I found the problems and have them all taken care of. But the situation did not improve a whole lot when running inside MATLAB. The JVM is still performing a large number of minor garbage collections and the process still runs for about 1 minute. So I have to download another external profiler and attach to the JVM launched by MATLAB. Then it became apparent... MATLAB invokes com.mathworks.jmi.OpaqueJavaInterface.doesMethodExist() whenever a java method is invoked which generates a large amount of temporary objects of class java.lang.Appendable (200Mbyte+ in my case). So then I changed the above call to minimize the number of 
calls to the java object (from 55000 to less than 50), the entire process takes less than 10 seconds. 

So the lesson learned... If you have a large number of java method calls, find a way to minimize them because MATLAB invoke java methods is very expensive in terms of temporary objects creation.