Delete TH1 and TFile: memory consumption increases

Dear experts

The following function inside an event loop is making the memory consumption to increase:

Double_t S0percentile(Int_t nCharged, Double_t S0)
{

  TFile *InputFile = new TFile("S0classes.root", "read");
  TH1D *h = (TH1D *)InputFile->Get(Form("hProjYMult%dnew",nCharged));
  Int_t S0bin = h->GetXaxis()->FindBin(S0);
  Double_t S0percent = h->GetBinContent(S0bin);
  delete h;
  InputFile->Close();
  delete InputFile;
  
  return S0percent;
  
}

Where S0classes.root is a root file that contains many TH1 objects. The idea is that this root file has to be read for each event, choose a particular TH1 and extract some information. I think Im correctly deleting the histogram and the file, but I dont know why this function creates a memory leak.

Thanks for your help!

Im using Root v6-14-02, Pythia v8.235 and Ubuntu 18.04.3 LTS

There are no obvious leak in that code. Did you verify that it is ‘that’ routine that is introducing the leak? (how did you do this verification?)

If I coment out the function everything works perfect. Indeed, the dummy function

Double_t S0percentile(Int_t nCharged, Double_t S0)
{

TFile* InputFile = nullptr;
TH1D* h = nullptr;

Double_t S0percent = 0.0;
delete h;
delete InputFile;

return S0percent;

}

Works OK, indicating that Tfile and the TH1 are being deleted correctly. Furthermore if I include, in the previous dummy function, the line

InputFile = TFile::Open(“S0classes.root”);

Then the memory consumption starts to increase, but I dont understand why. This line indicates that the root file is being opened and deleted for each event.

Regards,
Lizardo

PS. I dont know if it is related, but Im using this routine within a Pythia simulation.

PS. I dont know if it is related, but Im using this routine within a Pythia simulation.

Can you try that function outside of the Pythia simulation?

Exactly the same happens in CRMC that reads a simulation output from a .hepmc file
Should I test this function in an only-Root code? If so, can you point me to a example (inside $ROOTSYS?) where I can do it?

Regards,

you could create a file runmany.C containing just:

Double_t S0percentile(Int_t nCharged, Double_t S0)
{

  TFile *InputFile = new TFile("S0classes.root", "read");
  TH1D *h = (TH1D *)InputFile->Get(Form("hProjYMult%dnew",nCharged));
  Int_t S0bin = h->GetXaxis()->FindBin(S0);
  Double_t S0percent = h->GetBinContent(S0bin);
  delete h;
  InputFile->Close();
  delete InputFile;
  
  return S0percent;
  
}
void runmany(size_t howmany = 100) {
   for(size_t i = 0; i < howmany; ++i) {
      S0percentile(1,1);
  } 
}

and do

root.exe -b -l -q 'runmanyC(10000000)'

and see if the memory grows …

Try:

Double_t S0percentile(Int_t nCharged, Double_t S0) {
  Double_t v = 0.;
  TFile *f = TFile::Open("S0classes.root");
  if (!f || f->IsZombie()) { delete f; return v; } // just a precaution
  TH1D *h; f->GetObject(TString::Format("hProjYMult%dnew", nCharged), h);
  if (!h) { delete f; return v; } // just a precaution
  Int_t b = h->FindFixBin(S0); // may return under-/over-flow bins
  if (b >= 1 && b <= h->GetNbinsX()) v = h->GetBinContent(b);
  delete f; // automatically deletes "h", too
  return v;
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.