Login  Register

Re: memory usage buildup

Posted by JaapK on Jan 13, 2011; 8:10am
URL: http://imagej.273.s1.nabble.com/memory-usage-buildup-tp3686005p3686010.html

Hello again,

After some futile attempts involving a lot of redundant calls to
System.gc(), I solved the memory leak thanks to a day of diagnostics
with visualVM.

The leak occurred in a static function I wrote. It took an ImagePlus
with a Roi that had been set to it (using imp.setRoi(r) ) as an
argument. The function then got the ImageProcessor of the ImagePlus by
invoking imp.getProcessor(), and did some drawing operations on the
image, within the Roi. The function returned an integer.

This is what went wrong: in Java you are not supposed to pass object
by reference as I did with the ImagePlus object. Apparently some kind
of a ghost ImageProcessor or ImagePlus was created in the function
that could not be garbage collected.

I rewrote the function to accept an ImageProcessor and a Roi
separately and made it return a new ImageProcessor. Problem solved.

Thank you very much for all the advice.

Kind regards,
Jaap Kokorian

Op 13 januari 2011 09:09:05 UTC+1 heeft Jaap Kokorian
<[hidden email]> het volgende geschreven:

> Hello again,
>
> After some futile attempts involving a lot of redundant calls to System.gc(), I solved the memory leak thanks to a day of diagnostics with visualVM.
>
> The leak occurred in a static function I wrote that took an ImagePlus with a roi that had been set to it (using imp.setRoi(r) ) as an argument. The function the got the ImageProcessor of the ImagePlus by invoking imp.getProcessor(), and did some drawing operations on the image within the roi. The function returned an integer.
>
> This is what went wrong: in Java you are not supposed to pass object by reference as I did with the ImagePlus object. Apparently some kind of a ghost ImageProcessor or ImagePlus was created in the function that could not be garbage collected.
>
> I rewrote the function to accept an ImageProcessor and a Roi separately and made it return a new ImageProcessor. Problem solved.
>
> Thank you very much for all the advice.
>
> Kind regards,
> Jaap Kokorian
>
>
> 2011/1/5 Jaap Kokorian <[hidden email]>:
>> I have run the garbage collector at the start of every iteration and I
>> set all imageprocessors, imageplus's, particleanalyzers etc to "None"
>> manually (the jython equivalent of 'null'). Also, I used the visualVM
>> tool to check a bit what was going on.
>> I managed to decrease the buildup of memory, but I haven't been able
>> to stabilize it completely. It might be a 'non-proper' programming
>> thing after all. The script is becoming a bit cluttered anyway. I am
>> going to convert from jython to pure java and tidy it up in the
>> process. Hopefully that will solve it.
>>
>> Thanks for your support!
>>
>> Regards,
>> Jaap Kokorian
>>
>> 2011/1/3 Michael Doube <[hidden email]>:
>>>> when you click on the status bar, ImageJ does
>>>> garbage collection which works great.  In long macros with batch
>>>> processing I have the macro call the garbage collection plugin which
>>>> you can get on the ImageJ website.  You need to call it a few times.
>>>
>>> For sure, if that works then ignore my suggestion, which is much more
>>> complicated. If you find that your plugin is resistant to manual garbage
>>> collection, you'll need to probe deeper.
>>>
>>> Michael
>>>
>>
>
>