From: Ragaar on
<version>
MATLAB Version 7.4.0.287 (R2007a)
Java VM Version: Java 1.5.0_07 with Sun Microsystems Inc. Java
HotSpot(TM) Client VM mixed mode
</version>

I have an application that is pulling a large number of XML files and
then attempts to parse out the value for a single element. I ended up
gutting the internal function xmlread() in hopes of minimizing
unnecessary java calls. No matter what I do the function seems to run
into a heap overflow.

<code>
% java calls:
DocumentBuilderFactory =
javaMethod('newInstance','javax.xml.parsers.DocumentBuilderFactory');
javaMethod('setValidating',DocumentBuilderFactory,0);

DocumentBuilder=javaMethod('newDocumentBuilder',DocumentBuilderFactory);

%
DocumentBuilder.parse(file).getElementsByTagname('double').item[0].TextContent
xerces=javaMethod('parse',DocumentBuilder,file);
element=javaMethod('getElementsByTagName',xerces,'double');
item=javaMethod('item',element,0);

value(i)=str2double(get(item,'TextContent'));
</code>

I have been searching for information provided to other people with
similar issues. But, it appears that there is only a single fix.
Increasing the heap size by creating a java.opts file
REF: http://www.mathworks.com/support/solutions/en/data/1-18I2C/index.html
REF: http://blogs.mathworks.com/desktop/2010/04/26/controlling-the-java-heap-size/#comment-6988

Unfortunately, increasing the heap size hasn't resolved my problem.
And even if it had I feel it is an attempt to workaround the real
issue.
"There can be many different causes: memory leaks, unnecessary
leftover objects, too many concurrently-active components, inefficient
memory hibernation etc."

I have other methods in mind for handling the XML files; but, I think
it would be beneficial to try and work through this problem.

Some directions I have considered pursuing include:
Push a GarbageCollection event,
Applying different java.opts switches

Thanks,
Ragaar
From: Ragaar Ashnod on
I don't have what I would consider an ideal solution. But, I will document what I did for any future readers.

I was originally calling dynamic URLs which provided an XML output which were then processed for a single value.

DocumentBuilder.parse(URL) is possible. However, after parsing a few thousand the process would fail. I decided to use an external program for downloading the various URL links and parse them locally; with the intent of removing the overhead for HTTP request handling.
Unfortunately, DocumentBuilder.parse(localFile) still causes Matlab to crash after a few thousand calls. Therefore, the files are now parsed as a string with something such as textscan() or regexp().