<div dir="ltr"><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small">Hi Tim,</div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small">the memory is released in destructor, and the easiest way to trigger it is to do registration in a function or an unnamed block (example attached).</div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small">Regards,</div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small">Dženan</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Sep 6, 2016 at 12:34 PM, Timothee Evain <span dir="ltr"><<a href="mailto:tevain@telecom-paristech.fr" target="_blank">tevain@telecom-paristech.fr</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Dženan,<br>
<br>
Your fix works like a charm. The process take now ~4Gb(in release).<br>
I observed that this memory isn't released after the end of the registration process. Is that intended ?<br>
Also, to write it down for potential users who want to patch their 4.10 version or earlier, there is a small macro to change (ITK_CONSTEXPR_VAR into ITK_CONSTEXPR) since master branch had a modification about macros.<br>
<br>
Anyway, many thanks to you for the swift help ! :)<br>
<span class=""><br>
Tim<br>
<br>
<br>
<br>
----- Mail original -----<br>
De: "Dženan Zukić" <<a href="mailto:dzenanz@gmail.com">dzenanz@gmail.com</a>><br>
À: "Timothee Evain" <<a href="mailto:tevain@telecom-paristech.fr">tevain@telecom-paristech.fr</a>><br>
Cc: <a href="mailto:community@itk.org">community@itk.org</a><br>
</span>Envoyé: Mardi 6 Septembre 2016 17:43:49<br>
<span class="im HOEnZb">Objet: Re: [ITK] Bspline registration with Mutual Information v4 : memory concern<br>
<br>
Hi Tim,<br>
<br>
</span><div class="HOEnZb"><div class="h5">I did some memory profiling using 4.9.0 (which also exhibits the problem).<br>
With current code, the MattesMutualInformationImageTo<wbr>ImageMetricv4::<br>
DerivativeBufferManager::<wbr>DoubleBufferSize() increases the buffer to huge<br>
sizes, because CheckAndReduceIfNecessary() fails to acquire the<br>
synchronization lock. The reason is probably that memory-bound combining<br>
the PDF derivatives is slower than its CPU-bound calculation.<br>
<br>
In release mode, the buffers grew to 50GB before I killed the process. In<br>
debug mode, they only grow to around 5GB, again probably because the<br>
calculation speed is on par with PDF combining due to debug overhead.<br>
<br>
Can you review the proposed fix?<br>
<a href="http://review.source.kitware.com/21501" rel="noreferrer" target="_blank">http://review.source.kitware.<wbr>com/21501</a><br>
<br>
Regards,<br>
Dženan<br>
<br>
On Tue, Sep 6, 2016 at 5:32 AM, Timothee Evain <<a href="mailto:tevain@telecom-paristech.fr">tevain@telecom-paristech.fr</a>><br>
wrote:<br>
<br>
> Hi Dženan,<br>
><br>
> Sure!<br>
> You could find here an archive <a href="https://www.dropbox.com/s/9scgn9if39i4a1x/" rel="noreferrer" target="_blank">https://www.dropbox.com/s/<wbr>9scgn9if39i4a1x/</a><br>
> V4RegistrationMemoryLeak.tar?<wbr>dl=0<br>
> It contains:<br>
> -Fixed and moving images in mhd format<br>
> -The binary (for windows)<br>
> -The source code with its CMakeLists if you want to compile for another OS<br>
><br>
> Thanks,<br>
><br>
> Tim<br>
><br>
><br>
> ----- Mail original -----<br>
> De: "Dženan Zukić" <<a href="mailto:dzenanz@gmail.com">dzenanz@gmail.com</a>><br>
> À: "Timothee Evain" <<a href="mailto:tevain@telecom-paristech.fr">tevain@telecom-paristech.fr</a>><br>
> Cc: <a href="mailto:community@itk.org">community@itk.org</a><br>
> Envoyé: Lundi 5 Septembre 2016 20:54:29<br>
> Objet: Re: [ITK] Bspline registration with Mutual Information v4 : memory<br>
> concern<br>
><br>
> Hi Tim,<br>
><br>
> can you turn this snippet into a runnable example which reproduces the<br>
> memory leak?<br>
><br>
> Regards,<br>
> Dženan<br>
><br>
> On Mon, Sep 5, 2016 at 12:27 PM, Timothee Evain <<br>
> <a href="mailto:tevain@telecom-paristech.fr">tevain@telecom-paristech.fr</a><br>
> > wrote:<br>
><br>
> > Hello everyone,<br>
> ><br>
> > I'm trying a Bspline registration based on the Mutual Information metric<br>
> > (v4) and I'm facing a memory problem.<br>
> > When running the registration, the memory usage skyrockets in a second<br>
> > until freezing the PC by lack of memory.<br>
> > I don't know why : this doesn't happen when I'm switching to another<br>
> > metric (e.g. Correlation), the images aren't that big (~400k voxels) and<br>
> a<br>
> > previous rigid registration phase behaved correctly.<br>
> > Here is my code snippet, what am I doing wrong ? (Image overlap had been<br>
> > ensured)<br>
> ><br>
> > typedef itk::Image<float,3> ImageTypef;<br>
> ><br>
> > itk::<wbr>ImageRegistrationMethodv4<<wbr>ImageTypef,ImageTypef>::<wbr>Pointer<br>
> > WarpHelper=<br>
> > itk::<wbr>ImageRegistrationMethodv4<<wbr>ImageTypef,ImageTypef>::New();<br>
> > itk::<wbr>MattesMutualInformationImageTo<wbr>ImageMetricv4<ImageTypef,<br>
> ImageTypef>::Pointer<br>
> > MIMetrics=<br>
> > itk::<wbr>MattesMutualInformationImageTo<wbr>ImageMetricv4<ImageTypef,<br>
> > ImageTypef>::New();<br>
> > itk::BSplineTransform<double,<wbr>3,3>::Pointer BSplineTransform=<br>
> > itk::BSplineTransform<double,<wbr>3,3>::New();<br>
> > itk::LBFGSOptimizerv4::Pointer Optimizer=<br>
> > itk::LBFGSOptimizerv4::New();<br>
> ><br>
> > MIMetrics-><wbr>SetNumberOfHistogramBins(50);<br>
> > MIMetrics-><wbr>SetUseMovingImageGradientFilte<wbr>r(false);<br>
> > MIMetrics-><wbr>SetUseFixedImageGradientFilter<wbr>(false);<br>
> > MIMetrics-><wbr>SetUseFixedSampledPointSet(<wbr>false);<br>
> > MIMetrics-><wbr>SetVirtualDomainFromImage(<wbr>TestIm);<br>
> ><br>
> ><br>
> > WarpHelper->SetFixedImage(<wbr>TestIm);<br>
> > WarpHelper-><wbr>SetMetricSamplingPercentage(0.<wbr>002);<br>
> > WarpHelper->SetMovingImage(<wbr>MovIm);<br>
> > WarpHelper->SetMetric(<wbr>MIMetrics);<br>
> > //WarpHelper-><wbr>SetMetricSamplingStrategy(<wbr>WarpHelper->RANDOM);<br>
> > WarpHelper->InPlaceOn();<br>
> > WarpHelper-><wbr>SetInitialTransform(<wbr>BSplineTransform);<br>
> > WarpHelper->SetOptimizer(<wbr>Optimizer);<br>
> ><br>
> > itk::Array<double> ShrinkFactorPerLevel(1);<br>
> > ShrinkFactorPerLevel[0]=1;<br>
> > itk::Array<double> SmoothSigmaPerLevel(1);<br>
> > SmoothSigmaPerLevel[0]=0;<br>
> > WarpHelper->SetNumberOfLevels(<wbr>1);<br>
> > WarpHelper-><wbr>SetSmoothingSigmasPerLevel(<wbr>SmoothSigmaPerLevel);<br>
> > WarpHelper-><wbr>SetShrinkFactorsPerLevel(<wbr>ShrinkFactorPerLevel);<br>
> ><br>
> > /* ----- Initialize the Bspline Transform ----- */<br>
> > BSplineTransform-><wbr>SetTransformDomainOrigin(<wbr>TestIm->GetOrigin());<br>
> > BSplineTransform-><wbr>SetTransformDomainDirection(<br>
> TestIm->GetDirection());<br>
> ><br>
> > itk::Vector<double,3> PhysicalDim;<br>
> > for (int dim = 0; dim < 3; dim++)<br>
> > {<br>
> > PhysicalDim=(TestIm-><wbr>GetSpacing()[dim])*float((<wbr>TestIm-><br>
> > GetLargestPossibleRegion().<wbr>GetSize()[dim])-1);<br>
> > }<br>
> > BSplineTransform-><wbr>SetTransformDomainPhysicalDime<wbr>nsions(PhysicalDim);<br>
> ><br>
> > itk::Size<3> meshSize;<br>
> > meshSize.Fill(10-3);// NbGridNodesEachDim-SplineOrder<br>
> > BSplineTransform-><wbr>SetTransformDomainMeshSize(<wbr>meshSize);<br>
> ><br>
> > BSplineTransform->SetIdentity(<wbr>);<br>
> ><br>
> > /* ----- Optimizer setup ----- */<br>
> > itk::Array<double> ScaleBspline(BSplineTransform-<br>
> > >GetNumberOfParameters());<br>
> > ScaleBspline.fill(1);<br>
> ><br>
> > Optimizer-><wbr>SetLineSearchAccuracy(0.9);<br>
> > Optimizer-><wbr>SetDefaultStepLength(1.5);<br>
> > Optimizer->TraceOn();<br>
> > Optimizer-><wbr>SetGradientConvergenceToleranc<wbr>e(1e-3);<br>
> > Optimizer-><wbr>SetMaximumNumberOfFunctionEval<wbr>uations(20);<br>
> > Optimizer-><wbr>SetDoEstimateScales(false);<br>
> > Optimizer->SetScales(<wbr>ScaleBspline);<br>
> ><br>
> ><br>
> > try<br>
> > {<br>
> > WarpHelper->Update();<br>
> > }<br>
> > catch (itk::ExceptionObject &err)<br>
> > {<br>
> > std::cerr<<err;<br>
> > return 0;<br>
> > }<br>
> ><br>
> > Thanks,<br>
> ><br>
> > Tim<br>
> > ______________________________<wbr>_________________<br>
> > Community mailing list<br>
> > <a href="mailto:Community@itk.org">Community@itk.org</a><br>
> > <a href="http://public.kitware.com/mailman/listinfo/community" rel="noreferrer" target="_blank">http://public.kitware.com/<wbr>mailman/listinfo/community</a><br>
> ><br>
><br>
</div></div></blockquote></div><br></div>