[vtkusers] How to prevent out-of-memory crash with vtkImageReader from Java

Sebastien Jourdain sebastien.jourdain at kitware.com
Wed Oct 22 11:25:54 EDT 2014

Hi Marco,

I'm not an expert in that area, but if you try on your C++ end to allocate
the estimate memory amount and free it, you might be able to catch and
handle any kind of error feedback the way you want.

But also another approach which will be less exact will be to check what
remaining memory is available on the system and base on the file size,
pre-decide if that file will fit in or not. That option can probably just
live in Java. In fact, you can just let the user know, that loading such a
file may result of a crash as not enough memory might not be available.


On Wed, Oct 22, 2014 at 4:39 AM, Marco Sambin <m.sambin at gmail.com> wrote:

> Hi Seb,
> first of all, thanks for your feedback.
> I have taken a look at the C++ code of vtkImageReader: it seems to me that
> main allocation happens inside ExecuteDataWithInformation() method, which
> in turn calls vtkImageData::AllocateScalars(), which in turn
> calls vtkDataArray::CreateDataArray(), which finally I think ends up
> calling ::New() on the typed data array (probably, vtkUnsignedShortArray in
> my case).
> If I were to write my own image reader, for instance, I am not sure how I
> could detect eventual out-of-memory errors, since they would actually
> happen inside other VTK classes. Maybe some try-catch blocks in my own
> image reader may help me?
> Any suggestion would be greatly appreciated.
> I am looking forward to hearing from you.
> Thanks and best regards,
> Marco Sambin
> On Wed, Oct 15, 2014 at 4:30 PM, Sebastien Jourdain <
> sebastien.jourdain at kitware.com> wrote:
>> Hi Marco,
>> I don't have the answer but if there is no solution as today, maybe
>> adding another method on the reader that could first validate that enough
>> memory is available, might be an option. Specially in Java when such call
>> make the JVM crash.
>> That change will need to happen on the C++ side. But at that point I'm
>> not sure how to check that on the C++ side either. Maybe Berk, Dave or Brad
>> might have some insight.
>> Thanks,
>> Seb
>> On Wed, Oct 15, 2014 at 3:04 AM, Marco Sambin <m.sambin at gmail.com> wrote:
>>> Hi all.
>>> In my VTK-based Java application, I am reading a "raw volume file"
>>> (containing a set of CT slices) through some Java code looking like this:
>>> [...]
>>> vtkImageReader imageReaderVTK =  new vtkImageReader();
>>> imageReaderVTK.FileLowerLeftOn();
>>> imageReaderVTK.SetFileDimensionality(3);
>>> imageReaderVTK.SetFileName(curRawVolFile.getAbsolutePath());
>>> imageReaderVTK.SetDataExtent(0, sliceCols - 1,
>>>                       0, sliceRows - 1,
>>>                       0, numOfUsedImages - 1);
>>> imageReaderVTK.SetDataSpacing(xSpacing, ySpacing, zSpacing);
>>> imageReaderVTK.SetDataOrigin(0.0, 0.0, 0.0);
>>> imageReaderVTK.SetDataScalarTypeToUnsignedShort();
>>> imageReaderVTK.SetDataByteOrderToBigEndian();
>>> // The following call may cause a process crash due to out-of-memory
>>> imageReaderVTK.UpdateWholeExtent();
>>> [...]
>>> I am then passing this volume to a vtkImagePlaneWidget for some
>>> reslicing.
>>> As the last comment (in the code fragment above) suggests, in some cases
>>> I get a crash of the JVM process, due to the fact the reader goes out of
>>> memory while actually reading and allocating the whole volume in memory.
>>> This especially happens when using a 32-bit JRE.
>>> I understand that I cannot read a huge volume in memory if just a small
>>> amount of memory is available to the process, but my question is:
>>> is there a way to manage "more gracefully" this out-of-memory from Java
>>> code, in such a way to be able to inform the user that "Not enough memory
>>> is available to read this volume", instead of causing a JVM crash?
>>> I am using VTK 6.1.0.
>>> Thanks for any hint/suggestion about this topic.
>>> Best regards,
>>> Marco
>>> _______________________________________________
>>> Powered by www.kitware.com
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>> Please keep messages on-topic and check the VTK FAQ at:
>>> http://www.vtk.org/Wiki/VTK_FAQ
>>> Follow this link to subscribe/unsubscribe:
>>> http://public.kitware.com/mailman/listinfo/vtkusers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20141022/8e6dab6a/attachment.html>

More information about the vtkusers mailing list