Memory leak while looping on root file

Hi rooters,
I have some memory leak problems with my program that I can’t explain.
The code is very trivial, I have to loop on a list of root files containing TTrees and fill some histograms. The problem is that when I execute it on 3-4 files, I get a “killed” message after some minutes. If I run the program with just 1-2 input files it works.
The size of each input file is around 3-4 GB.
I report here the core of the program:

> int main(int argc, char * argv[]) { 
> 
>   // variables declaration
>   Double_t energy;
>   etc.
> 
>   // histogram declaration
>   TH1D *h1 = new TH1D("","",NBIN, xbins);
>   TH1D *h2 = new TH1D("","",NBIN, xbins);
>   TH1D *h3 = new TH1D("","",NBIN, xbins);
>    etc.
>   // arvg[1] is a txt file with the list of input root files  
>   std::ifstream fin(argv[1], std::ifstream::in); 
>   std::string fname;    
> 
>   while(1) {                                                                                                                     
>       fin >> fname;                                                                                                                                                                                      
>       if(fin.eof())                                                                                                               
>          break;  
>       TFile *rootfin = new TFile(fname.c_str(), "read");                                                                          
>       TTree *lev2 = (TTree*)rootfin->Get("TTreelev2");                                                                                 
>                
>       //set branch addresses                                                                                                                   
>       lev2->SetBranchAddress("energy", &energy);                                                                     
>       etc ...  
>      
>       int nent = lev2->GetEntries();                                                                                            
>       for(int iev=0; iev<nent ; ++iev)                                                                                            
>       {                                                                                                                           
>          lev2->GetEntry(iev);  
>          h1->Fill(energy);
>          etc.
>        }
>       delete lev2;  // unnecessary because the rootfin->Close()                                                         
>       rootfin->Close();  
>  }
>  TFile fout(argv[2],"recreate");  
>   h1->Write();
>   etc.
>   fout.Close();

Am I missing something about object ownership or am I making some c++ mistake?
I can’t explain why this need so much memory.

Try to replace rootfin->Close(); with delete rootfin;

Also, your histograms need some names, e.g.:

TH1D *h1 = new TH1D("h_1", "", NBIN, xbins);
TH1D *h2 = new TH1D("h_2", "", NBIN, xbins);
TH1D *h3 = new TH1D("h_3", "", NBIN, xbins);

Thanks for the answer but it doesn’t fix the problem :frowning: the program was killed anyhow.

I can’t understand why, if the memory is correctly released after each file is closed, there is a difference if I ran on 2 files or 20.

Try:


if (!(fname.size())) continue; // just a precaution
TFile *rootfin = TFile::Open(fname.c_str());
if ((!rootfin) || rootfin->IsZombie()) { delete rootfin; continue; } // just a precaution
TTree *lev2; rootfin->GetObject("TTreelev2", lev2);
if (!lev2) { delete rootfin; continue; } // just a precaution

Hi,
as @Wile_E_Coyote says, you have a new TFile without a corresponding delete, and that’s the only visible memory leak.

If adding a delete rootfin doesn’t fix the memory leak, you can check what is it that is leaked by compiling your program with debug symbols and running it through valgrind with

valgrind --track-origins=yes --suppressions=$ROOTSYS/etc/valgrind-root.supp ./yourprogram

Cheers,
Enrico

Unfortunately adding the delete didn’t fix the problem. I will try with valgrind, thanks for the suggestion.

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