Domanda

I have many volumes stored as .mha files which I usually open in Matlab by using a function called mha_read_volume, which you can find on Matlab Central. My problem is this function randomly throws an out-of-memory error message related to the decompressing of these images with some java library.

The culprit seems to lie here:

function M = zlib_decompress(Z,DataType)
import com.mathworks.mlwidgets.io.InterruptibleStreamCopier
a=java.io.ByteArrayInputStream(Z);
b=java.util.zip.InflaterInputStream(a);
isc = InterruptibleStreamCopier.getInterruptibleStreamCopier;
c = java.io.ByteArrayOutputStream;
isc.copyStream(b,c);
M=typecast(c.toByteArray,DataType);

I suspect a memory leak, but I have no idea what to do with this code to fix the problem.

Here is the error message:

??? Java exception occurred:
java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Unknown Source)
    at java.io.ByteArrayOutputStream.toByteArray(Unknown Source)

Error in ==> mha_read_volume>zlib_decompress at 87
M=typecast(c.toByteArray,DataType);

Error in ==> mha_read_volume at 74
        V = zlib_decompress(Z,DataType);
È stato utile?

Soluzione 2

If the files you read are large (their uncompressed version) this does not need to be a memory leak. You can adjust the java heap space matlab uses in the matlab preferences. Usually it is set to a rather small value of 128 MB - increase it and see if the problem persists.

To check whether it is a memory leak, simply try something along:

for i=1:1000 % adjust loop-number to your needs
m = zlib_decompress(...)
end

If there is a leak, this should error out once your heap space is full again. If not - your memory footprint should stay within certain limits.

Altri suggerimenti

It is important to close streams when job is over to free memory. Use try/catch to ensure resources are freed even in case of exceptions.

So you should invoke c.close b.close a.close to be able to process large number of files in loop.

Remark (not related to your situation): Java 8 is known to leak native memory if java.util.zip.InflaterInputStream streams are not closed. The reason is the replacement of pure-Java implementation by Hotspot calls to zlib.

I might be late to answer this but this memory problem can be solved by adding the file java.opts in the current directory of Matlab workspace where you can specifiy the used memory by Matlab:

http://de.mathworks.com/matlabcentral/answers/92813-how-do-i-increase-the-heap-space-for-the-java-vm-in-matlab-6-0-r12-and-later-versions

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top