I have problems with large memeory consumption which causes my progra[/code]m to crash when it’s being executed. Unfortunately have I not been able to find it myself and and I hope there is somebody out there who can help me.
I have to read in (in a loop) a large number of root files and loop over a TTree in each of the files. The TTree consists of only one home made object which contains STL containers (std::vector). Each time a new file is opened the consumed memory increases by approx. 10 MB, even though I close the file and delete the tree at the end of each loop. It seems like somewhere some memory is lost…
Below some pseudo-code how I open and close the TFile and access the TTree. Is there anything wrong?
I am happy for any suggestions, cheers Bjarte
Ps: I use root version 3.10 and gcc 3.2.2
int doAnalysis(TString& inFile, TString& outFile){
// Open input file
TFile* fInFile = new TFile(inFile,“read”);
if(!fInFile) {
cout << “Can not open input file!” << endl;
return 1;
}
TTree* uuEventTree = (TTree*) fInFile->Get(“UUeventTree”);
if(!uuEventTree){
cout << “can not access tree in input file” << endl;
return 1;
}
// Prepare UUeventTree
UUevent* uuEvent = new UUevent;
uuEventTree->SetBranchAddress(“UUeventBranch”,&uuEvent);
// Start main loop
Int_t nentries = Int_t(uuEventTree->GetEntries());
for (Int_t jentry=0; jentry<nentries;jentry++) {
uuEventTree->GetEvent(jentry);
//Do analysis here
//
Thanx for your reply, you’re right, I have forgotten to delete the uuEvent pointer. Unfortunately does this not seem to solve my problem and my memory consumption is still growing as I loop over my files.
If this is code in a macro, perhaps you can ask CINT to force a garbage collection. Alternatively: you don’t have cyclic pointer references by any chance?
I think modern garbage collectors should handle these things by now but you never know.
I am afraid that “a modern garbage collector” will not solve this problem. :
A memory leak when reading a tree happens when the destructor of the top level class in the Tree does not delete all the sub-objects.
A typical mistake is the deletion of a container (eg TObjArray, std::vector*) without deleting the objects in the array.
eg with a TObjarray, it is not sufficient to do
delete array;
one must do:
array.Delete();
delete array;
unless array has the SetOwner flag set.
a good way for tracking memory leaks in a tree analysis is to print the number of all class instances for each event and compare them event-wise. This is done by using ‘gObjectTable->Print()’.