Hello, rooter.
I am a student who is writing a Geant4 simulation program with ROOT framework.
I am using TClonesArray to store each step information of the simulation, with step class which I made.
To save this information for each event, I build a tree in a TFile, which has a branch for the TClonesArray of my step class.
However, the memory usage is too large so that the memory of my computer runs out easily.
Of course, I clean all of the objects at the end of an event, which I created during the processing of steps. So I expected the memory usage will reduce when the objects cleaned. But, It does not so.
I think there are some memory leaks in the TTree and TClonesArray. So, I performed some experiments to confirm my postulate. The step of the experiment is as follows:
- Create a .root file and build a TTree in the file.
- Then, make a TClonesArray which stores TObjString class and register this array as a branch.
- Fill the array with test string for a big number (I filled 10000000 times)
- Fill the tree
- Delete an object of the TClonesArray.
- Delete an object of the TTree.
The result is that the memory usage did not reduce even the tree has been destroyed (after delete fTree;)
If I do not use the TTree and TFile, it seems that the memory is freed when the object of TClonesArray is destroyed.
Here is my source code for the experiment.
#include "TClonesArray.h"
#include "TTree.h"
#include "TFile.h"
#include "TObjString.h"
#include <iostream>
using namespace std;
int main()
{
Int_t entries = 10000000;
TClonesArray* arr = new TClonesArray("TObjString",10000000);
TTree* fTree;
TFile* fOutput;
TObjString targ("test_string");
fOutput = new TFile("testin.root","NEW");
fTree = new TTree("dummy","dummy");
fTree->Branch("dummy",&arr);
for(int i = 0; i<entries; i++){
¦ new ((*arr)[i]) TObjString(targ);
}
cout << "End of loop" << endl;
cout << "filling the tree" << endl;
fTree->Fill();
cout << "filling done. " << endl;
cout << "Before destruction. " << endl;
cin.get();
delete arr;
cout << "Destruction done. " << endl;
cin.get();
fOutput->Close();
delete fOutput;
cout << "File Closed. " << endl;
cin.get();
}