Root eating memory


I have a class in which I use a TH1F object all the time again and again, and the result is that somehow the memory of my pc just gets more less as it runs. I Fill the TH1F in a loop a lot of time and I need to empty it and reuse again. I don’t understand why it eats more and more memory when I declare my TH1F histo in the header of my class and I construct it in the constructor of my class and delelte it in the destructor. The only thing that happens is that I ‘empty’ the histo in this way:

histo->Add(histo, -1);

which is - I think - a bad solution for using the same histo a million times. So can you give a better solution for using the same histo a miilion times and without eating all my 512 Mbyte memory?

Thanx in advance,


Oh, the usual way:

TH1F * histo = (TH1F*)gROOT->FindObject(“histo”);
if(histo) delete histo;
histo = new TH1F(“histo”,“histo”, 100, 0, 100);

inside the loop, doesn’t work unfortunately.


can you send us a short script that we can just execute, that shows what you’re doing, and how it eats up all your mem?

Hi Balint,

try this one:

TH1F * histo = (TH1F*)gROOT->FindObject("histo");
if( histo != 0 ) {
   delete histo;
   histo = new TH1F("histo","histo", 100, 0, 100);

or just use


inside the loop.


where’s the loop? I now have to guess what you’re doing, to be able to reproduce your mem leak. My guess is attached, and it does not produce a mem leak. I’m running it as “.x test.C”.

Both of us could have saved some time if you had sent me a running macro that shows your mem leak…

[code]void test() {
TH1F * histo = new TH1F(“histo”,“histo”, 100, 0, 100);

while(( histo = (TH1F*)gROOT->FindObject(“histo”))) {
delete histo;
histo = new TH1F(“histo”,“histo”, 100, 0, 100);

Hi Axel,

Sorry for being late but there was no reply to my original question for more than 1 day, that is why I thought maybe it is a trivial problem and needs no answer.

Here is the code attached. I use my own classes in the script that needs to be loaded into Cint.

Thanx for advance, and sorry again.

SqlRoot.cxx (3.47 KB)
SqlRoot.h (876 Bytes)
testfitroot.C (1.75 KB)


You may start the scripts with : .x testfitroot.C(5, 5, 4000, 4500, 4000, 4500, 1600, 2150);

This script is about to connect ot a mysql server and get data from there, then fill a histo with them. After this I need to empty the histo, and reuse it again for the next data-set from the mysql server. You can see the server name, and a public user name with password in the script that allows one only to “select” from the database. If it doesn’t run it may be because of network problems.

I am not sure that the TH1F eats my memory, but I can’t figure out what memory allocation is the wrong one. Maybe I don’t delete something, I am not sure…



you need to delete the TSQLResults after using them. The server’s Query creates a new one each time you call it, and it’s the user’s responsibility to delete it.

Hi Alex,

Thanx for the tip! It works!!!:)))

Now, thanx for your help, the science can go on!:wink: