Deleting objects which remain in memory


I have a PyROOT program which crashes because it goes out of memory. Using ‘top’ in the shell I can see the MEM usage increasing constantly every time the program load a new file.

In the main loop the program mainly do two things:

  1. it opens a .root file, picks up some histos and add them to the previous ones. Then closes the file with f.Close().
  2. it adds the same file to a TChain, makes some tree.Projects() of branches and then the iteration is ended.

When looking for the cause of the mem leakage, I checked the various TLists of gROOT and I saw that:

  1. in gROOT.GetListOfFiles() all the files which had been opened and closed were still listed there.
  2. in gROOT.GetListOfSpecials() there were still all the tchains opened, one per each loop iteration
  3. in gROOT.GetListOfCleanups() there were as many objects “TTreePlayer”, as many tchains were created.

So actually in my loop the .root file is not deleted when calling f.Close(), and the TChain is not deleted when it goes out of scope at the end of the loop.

I tried to use the python “del” command at the end of the loop, but it did not work.
Then I tried to use SetOwnership() as suggested in other posts but it did not help, the objects still remained in the lists (tried both with True and False, just to be sure!). I also tried to set the kMustCleanup bit for the objects, but it didn’t help either.

So, to try to clean up I deleted by hand the TFile and TChain objects from the TLists above, before each iteration; I used .Close() for the files (here apparently it worked, but not in the main loop…), and I used gROOT.GetListOfSpecials().Clear() to get rid of the dangling TChains.

…but I still cannot get rid of the TTreePlayer instances in the Cleanup list! I tried to loop over them: the python ‘del’ does not help. And calling the .Delete() command over the listed TTreePlayer objs causes a crash. I guess I should delete the TTreePlayer objects in some other list…but where are they? I could not find them.

and the memory usage is still increasing at each iteration, because each time a new TTreePlayer instance is added to the list…

Please notice that the program runs perfectly fine when handling few files. The memory, still increasing, cannot explode and the programs works. It crashes only when I run on more files.

Thanks a lot for any help or hint you can give me :slight_smile:

Have a nice day!



I’m using ROOT 5.18 on Linux SLC4

thanks again for your help!!


I just migrated, but I observe the same behaviour with ROOT 5.27 on Linux SLC5.


I just tried to implement something that does as you described, to see whether I could reproduce, but I could not. I could try further, but it’d be easier if you could upload some sample code. Thanks.