Reduce virtual memory

Hi Rooters,
I have a Macro compiled in ACLIC and the virtual memory becomes really huge after a number of iterations … :cry:
I have a TTree from which I fill a gif (animated).

I put the code in attachment

[code]#include .
#include
#include “TMath.h”
#include “TTree.h”
#include “TFile.h”
#include “TH1.h”
#include “TH3.h”
#include “TH1F.h”
#include “TGraph.h”
#include “TGraphSmooth.h”
#include “TSpectrum.h”
#include “TCanvas.h”
#include “TStyle.h”

void Anim(char rfile[100],char outname[100])
{
gStyle->SetOptStat(0);

TCanvas *c1 = new TCanvas("c1","c1",600,600);

for(Int_t i=1;i<50;i++)
{
    TFile *f = new TFile(rfile);

    Int_t time=5*i;
    TTree *AllDetectors= new TTree();        
    cout<<"Time-- > "<<i<<" ns"<<endl;
    AllDetectors = (TTree*)gDirectory->Get("AllDetectors");
    AllDetectors->SetBranchStatus("*",0);
    AllDetectors->SetBranchStatus("Time",1);
    AllDetectors->SetBranchStatus("Part",1);
    AllDetectors->SetBranchStatus("X",1);
    AllDetectors->SetBranchStatus("Y",1);
    AllDetectors->SetBranchStatus("Z",1);
    

    TH3F *hproj = new TH3F("hproj",Form("Time : %d ns",time),
                           20,0,45,20,-1.e7,1.e7,20,-1.e7,1.e7);
    TH3F *hprojg = new TH3F("hprojg",Form("Time : %d ns",time),
                           20,0,45,20,-1.e7,1.e7,20,-1.e7,1.e7);
    TH3F *hprojn = new TH3F("hprojn",Form("Time : %d ns",time),
                           20,0,45,20,-1.e7,1.e7,20,-1.e7,1.e7);

    hprojg->SetMarkerColor(2);
    hprojn->SetMarkerColor(4);
    
    
    AllDetectors->Draw("X:Y:Z>>hproj",Form("Time<%d",time),"goff");
    AllDetectors->Draw("X:Y:Z>>hprojg",Form("Time<%d && Part==22",time),"goff");
    AllDetectors->Draw("X:Y:Z>>hprojn",Form("Time<%d && Part==2112",time),"goff");


    
    c1->Clear();
    gPad->SetPhi(-2);
    hproj->Draw("");
    hprojg->Draw("same");
    hprojn->Draw("same");

    c1->Update();        
    c1->Print(Form("%s+20",outname));

    AllDetectors->Delete();
    hproj->Delete();
    hprojg->Delete();
    hprojn->Delete();

    f->Close();
}
c1->Print(Form("%s++",outname));

}[/code]

Thanks !

Let’s first solve this obvious problem. Replace

TTree *AllDetectors= new TTree(); cout<<"Time-- > "<<i<<" ns"<<endl; AllDetectors = (TTree*)gDirectory->Get("AllDetectors"); by

TTree *AllDetectors=(TTree*)gDirectory->Get("AllDetectors");
and let us know

Rene

Thanks for the reply,
so after applying your correction, the situation hasn’t changed.
I might allocate some memory I never free and at each iteration the
virtual size gets larger and larger.

Next step: what happens if you remove the "c1->Print(…) statements ?
Could you tell us your estimated leak per iteration?
Which ROOT version are you using?
Did you try valgrind with the memory leak checker?
Could you post your ROOT file somewhere in a public readable area?

Rene

Hi Rene,
so apparently, when I remove the c1->Print(“name.gif+20”) statement,
the allocated memory size gets really smaller (like 5 times less, @ 10th iteration).
I put the file at the following web address :
polywww.in2p3.fr/activites/physi … gma_0.root

Thanks,
Hayg

Unfortunately, you do not indicate which version of ROOT you use. I do not see any leak with version 5.18 or newer.
Looking at your script, I understand that you want to display a particle shower in time slots. What you are currently doing is very slow and will not produce a nice graphical result. I suggest changing it in the way indicated in the attachment. It will be much better and at least 20 times faster.

Rene
leak2.C (2.15 KB)

the one I use (locally) is 5.18 and @ccin2p3 5.16 and in both case I saw a grow of the used virtual memory at each iteration. The program is stopped when it reached the virtual memory size “limit” (in batch mode).
Thanks for your script which indeed works much faster. =D>

When you access the object you have always the same number of entries.
So, if for example one decide to see evolution in a larger time range, I would be more convenient to have more entries. Is there a way to force that ?

Thanks,

Hayg

In the script leak2.C change the time cutoff from “Time<120” to whatever you need.

Could you quantify what wsa the memory leak per iteration in your old script?

Rene