RooSimultaneous::fitTo() memory leak?

Hello,

I have recently noticed a rather large memory usage associated with a script that I have written. The basic idea is that I generate a RooSimultaneous composed of 2D PDFs in (x,y). I then fit this RooSimultaneous to data (from a RooMCStudy) saving the result to a RooFitResult. Next I delete the RooSimultaneous, pull out the minNll() associated with the fit, and delete the RooFitResult. The problem is that after this is done I have a few parameters leftover in memory called “params”. Since I am doing this fit hundreds of times at different locations in my 2D space this leads to a very noticeable memory management issue. I’ve done a memory trace on the script and here is what I have come up with:

Objects allocated before creating my RooSimultaneous (0 objects):

Objects allocated after RooSimultaneous creation, but before fit:

List of RooFit objects allocated while trace active: 0x21a09f0 : RooSimultaneous - NullPdf 0x21a0e90 : RooSetProxy - !plotCoefNormSet 0x21974e0 : RooRealVar - nevents_1 0x21d7350 : RooRealVarSharedProperties - RooRealVarSharedProperties 0x218b150 : RooExtendPdf - exNullModel_1 0x7f0e4e682d54 : RooArgSet - owned components 0x7f0e4e682ed4 : RooArgSet - owned components
Objects allocated after RooSimultaneous fit to data:

List of RooFit objects allocated while trace active: 0x21a09f0 : RooSimultaneous - NullPdf 0x21a0e90 : RooSetProxy - !plotCoefNormSet 0x21974e0 : RooRealVar - nevents_1 0x21d7350 : RooRealVarSharedProperties - RooRealVarSharedProperties 0x218b150 : RooExtendPdf - exNullModel_1 0x7f0e4e682d54 : RooArgSet - owned components 0x7f0e4e682ed4 : RooArgSet - owned components 0x7f0e4e6843d4 : RooArgSet - params 0x7f0e4e684f14 : RooArgSet - params 0x7f0e4e685754 : RooArgSet - params 0x21848e0 : RooArgList - 0x1bb2a00 : RooArgList - 0x218d5d0 : RooRealVar - nevents_1 0x217e5e0 : RooArgList - 0x217e750 : RooRealVar - nevents_1
Objects allocated after RooSimultaneous delete:

List of RooFit objects allocated while trace active: 0x21d7350 : RooRealVarSharedProperties - RooRealVarSharedProperties 0x7f0e4e6843d4 : RooArgSet - params 0x7f0e4e684f14 : RooArgSet - params 0x7f0e4e685754 : RooArgSet - params 0x21848e0 : RooArgList - 0x1bb2a00 : RooArgList - 0x218d5d0 : RooRealVar - nevents_1 0x217e5e0 : RooArgList - 0x217e750 : RooRealVar - nevents_1
Objects allocated after RooFitResult delete;

0x21d7350 : RooRealVarSharedProperties - RooRealVarSharedProperties 0x7f0e4e6843d4 : RooArgSet - params 0x7f0e4e684f14 : RooArgSet - params 0x7f0e4e685754 : RooArgSet - params
So it looks like whatever variable “params” is, its not being deleted properly. Its not a variable that I am creating, but looks like it is being created in the process of the actual fit so I’m not sure how I have any control over its existence. If it’s any help, I’m doing my fit with the following commands:

RooFitResult *NullFitResult = NullPdf->fitTo(*data, Save(1), Range(*model.rangeName), PrintLevel(-1), SumW2Error(kFALSE), Strategy(0), Optimize(1));
I’ve tried with Strategy= 1 & 2 with similar results. Optimize= 0 seems to result in only 2 instances of “params”, but that doesnt solve the overall problem. My guess is that the issue is connected to how the fit does constant optimization, but after a couple hours trying to find where this occurs I’m hoping there’s a simpler solution. If it would help, let me know and I can put together a simple example of what I’m doing and post it.

Thanks for any help you can give!

Hello again,

As a brief note, this is still an issue in the latest release of Root v5.34.02, RooFit v3.54.

I decided I would give it another try to find out where this issue is coming from by rooting (no pun intended) through the RooFit code. I managed to track it all the way to RooRealIntegral. It seems that the call to RooRealIntegral::~RooRealIntegral() does not delete the _params variable. As a result I would end up with one “params” variable for each variable I am using (in my case my x,y variables and my pdf when setting Optimize(1), thus 3 which is what I’m seeing).

I believe this can be fixed by adding the line:

in RooRealIntegral::~RooRealIntegral().

Let me know if you disagree or if there is any reason why this shouldn’t be done or if I’m way off base. Thanks again!

EDIT: This bug has been fixed as of the trunk version of ROOT v5.34.03 where the above suggestion was implemented. Thanks Wouter!

Hi,

I am experienceing a similar problem with a simultaneous fit to data in multiple categories. Dumping the memory info it seems like there is a problem in RooCategorySharedProperties and RooRealVarSharedProperties.
I am attaching some standalone sample code that demonstrates this.

compile using: g++ -o memcheck memcheck.cxx $(root-config --ldflags --libs --cflags) -l RooFit -l RooFitCore

The ROOT version I used is 5.34/07

Thanks,
Lukas
memcheck.cxx (1.55 KB)