# Large memory consumption after GetXaxis()-\>SetBinLabel(...) is called for histograms

**URL:** <https://root-forum.cern.ch/t/large-memory-consumption-after-getxaxis-setbinlabel-is-called-for-histograms/63304>\
**Category:** Newbie\
**Created:** [April 5, 2025, 11:40am UTC](https://root-forum.cern.ch/t/large-memory-consumption-after-getxaxis-setbinlabel-is-called-for-histograms/63304 "2025-04-05T11:40:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![abilandz](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abilandz/32/30235_2.png) [@abilandz](https://root-forum.cern.ch/u/abilandz)\
**Post date:** [April 5, 2025, 11:40am UTC](https://root-forum.cern.ch/t/large-memory-consumption-after-getxaxis-setbinlabel-is-called-for-histograms/63304/1 "2025-04-05T11:40:04Z")

</div>

Dear ROOT Experts,

I am trying to optimize my analysis code written in ROOT, and after some profiling, I spotted there is a large memory consumption (~ 50 Mb) immediately after hist-\>GetXaxis()-\>SetBinLabel(…) is called.

You can reproduce this by compiling first the following trivial code snippet as a reference:

TProfile \*tmp = new TProfile(“tmp”, “tmp”, 1, 0., 1.);

The memory profile is in snapshot\_1.

 ![snapshot_1](https://root-forum.cern.ch/uploads/default/original/3X/c/5/c5945ec97dc6ce4ce4e1a8291a5638dc1afb1a17.png)

Then, please compile this one:

TProfile \*tmp = new TProfile(“tmp”, “tmp”, 1, 0., 1.);  
tmp-\>GetXaxis()-\>SetBinLabel(1, “a”);

The memory profile for this case is in snapshot\_2.

 ![snapshot_2](https://root-forum.cern.ch/uploads/default/original/3X/f/6/f6b037d39787f3c75da8a09940474681366e6ff8.png)

As you can see, there is a dramatic increase of 50 Mb, merely after one bin label is set.

Thanks!

Cheers,  
Ante

$ root --version  
ROOT Version: 6.32.06  
Built for linuxx8664gcc on Apr 03 2025, 03:21:56  
From tags/v6-32-06-alice1@v6-32-06-alice1

$ g++ --version  
g++ (GCC) 14.2.0

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 5, 2025, 6:39pm UTC](https://root-forum.cern.ch/t/large-memory-consumption-after-getxaxis-setbinlabel-is-called-for-histograms/63304/2 "2025-04-05T18:39:02Z")

</div>

Hi,

Thanks for sharing these details.  
could you please share the executable you are compiling?

Best,  
Danilo

---

<div class="post-metadata">

**Author:** ![abilandz](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abilandz/32/30235_2.png) [@abilandz](https://root-forum.cern.ch/u/abilandz)\
**Post date:** [April 6, 2025, 5:22am UTC](https://root-forum.cern.ch/t/large-memory-consumption-after-getxaxis-setbinlabel-is-called-for-histograms/63304/3 "2025-04-06T05:22:08Z")

</div>

Dear Danilo,

Thanks for your feedback!

I have stripped down the executable to the bare bones, here is the minimal code snippet in the file `BareBones.C` which nails down the problem:

```auto
#include <TProfile.h> 

int BareBones()
{
 TProfile *tmp = new TProfile("tmp", "tmp", 1, 0., 1.);
 tmp->GetXaxis()->SetBinLabel(1, "a");
 return 0;
}

int main()
{
 BareBones();
 return 0;
}

```

After initializing ROOT environment, I compile with:

```auto
g++ -o BareBones-exe BareBones.C `root-config --cflags --libs`

```

Then, I run valgrind with:

```auto
valgrind --tool=massif --time-unit=ms ./BareBones-exe

```

Finally, I visualize memory consumption with:

```auto
massif-visualizer massif.out.3759370

```

You need to replace 3759370 above with the PID you got after running valgrind.

Thanks!

Cheers,  
Ante

---

<div class="post-metadata">

**Author:** ![abilandz](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abilandz/32/30235_2.png) [@abilandz](https://root-forum.cern.ch/u/abilandz)\
**Post date:** [April 12, 2025, 9:48am UTC](https://root-forum.cern.ch/t/large-memory-consumption-after-getxaxis-setbinlabel-is-called-for-histograms/63304/4 "2025-04-12T09:48:23Z")

</div>

Dear Danilo, All,

There is also a similar memory blow-up after cloning:

```auto
  TH1F *hist = new TH1F("hist", "", 1, 0., 1.); // memory is < 1 MB
  TH1F *histClone = (TH1F*) hist->Clone("histClone"); // memory blows up to > 50 MB

```

After looking at the source code of SetBinLabel(…), the blow-up reported in the very first thread can be further traced down to:

```auto
THashList *hlist = new THashList(1,3);
TObjString *obj = new TObjString("a"); // by this point memory is still < 1 MB
hlist->Add(obj); // after this step memory goes up by 50 MB

```

Since the blow-up in both cases is ~ 50 MB, it feels strongly that the underlying cause is the same, but i didn’t dig deeper into the ROOT source code to confirm this… I leave that investigation kindly to the ROOT experts! 🙂

Thanks!

Cheers,  
Ante

---

<div class="post-metadata">

**Author:** ![abilandz](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abilandz/32/30235_2.png) [@abilandz](https://root-forum.cern.ch/u/abilandz)\
**Post date:** [April 15, 2025, 6:46am UTC](https://root-forum.cern.ch/t/large-memory-consumption-after-getxaxis-setbinlabel-is-called-for-histograms/63304/5 "2025-04-15T06:46:29Z")

</div>

Dear Danilo, All,

I am done with the profiling of my analysis code, and I have a further feedback which might be helpful in this context.

There are several other classes which are affected, I can confirm TF1, TFormula, and TFile, in a sense that:

```auto
 TFile *file = new TFile("file.root", "new"); // memory blows up to > 50 MB
 file->Close(); delete file; file = nullptr; // memory is NOT released back

 TF1 *f1 = new TF1("f1", "sin(x)", 0., 1.); // memory blows up to > 50 MB
 delete f1; // memory is NOT released back

```

However, TH2D works as expected:

```auto
 TH2D *hist = new TH2D("hist", "", 100, 0., 100., 2000, 0., 2000.); // memory peaks at ~2.5 MB
 delete hist; // memory is released back

```

It feels to me that everything is related to the underlying gROOT object, and all the automated bookkeeping it does behind the scene for some selected classes. Namely, even if I do not declare any object in my code, I can reproduce the problem with this trivial one-liner:

```auto
gROOT->GetListOfFunctions(); // memory blows up to > 50 MB

```

And perhaps the most extreme example:

```auto
std::cout << gROOT << std::endl; // memory blows up to > 50 MB

```

Thanks!

Cheers,  
Ante

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [April 16, 2025, 3:57pm UTC](https://root-forum.cern.ch/t/large-memory-consumption-after-getxaxis-setbinlabel-is-called-for-histograms/63304/6 "2025-04-16T15:57:32Z")

</div>

Maybe @pcanal can share some insight.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [April 21, 2025, 11:16am UTC](https://root-forum.cern.ch/t/large-memory-consumption-after-getxaxis-setbinlabel-is-called-for-histograms/63304/7 "2025-04-21T11:16:50Z")

</div>

In ROOT master (debug), the "peak I see goes down until 24,9 MB, maybe thanks to some optimizations done recently by @hahnjo in template loading in llvm/clang/cling.  
So I guess that when calling “gROOT”, cling gets initialized and all module-maps are being loaded. Maybe compiling with runtime\_cxxmodules = off reduces this a bit?

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [April 21, 2025, 5:51pm UTC](https://root-forum.cern.ch/t/large-memory-consumption-after-getxaxis-setbinlabel-is-called-for-histograms/63304/8 "2025-04-21T17:51:10Z")

</div>

> [@ferhue](#):
>
> So I guess that when calling “gROOT”,

Yes, the first time `gROOT` is called it the latest point until which we can delay the initialization of the `ROOT` environment which of course includes the interpreter (setting up the interpreter, creating the `gInterpreter` (`TCling`) object and this indeed requires the loading of a ‘minimal’ set of things which for the `runtime_cxxmodules` indeed includes attaching the module files. (Without `runtime_cxxmodules` instead we load the `rootmap` file and the contained forward declaration)

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [April 22, 2025, 7:38am UTC](https://root-forum.cern.ch/t/large-memory-consumption-after-getxaxis-setbinlabel-is-called-for-histograms/63304/9 "2025-04-22T07:38:30Z")

</div>

A quick hack is to rename the file /opt/root/include/ROOT.modulemap as “aaaa” and then the memory will go down from 34MB to 11MB in my setting calling gROOT. So at least 23 MB of the blowup are coming from the modules.  
This of course may break things, but if you just want to call SetBinLabel, it might work.

You will get these errors printed though:

```auto
fatal error: module map file '/opt/root/include//ROOT.modulemap' not found
Replaced symbol at_quick_exit cannot be found in JIT!
Module ROOT_Foundation_C not found.
Module ROOT_Config not found.
Module ROOT_Rtypes not found.
Module ROOT_Foundation_Stage1_NoRTTI not found.
Module Core not found.
Module Rint not found.
Module RIO not found.
Module MathCore not found.
Module Hist not found.
Module Core not found.
Module Thread not found.
Module RIO not found.
Module TMVAGui not found.
Module TMVA not found.
Module ROOTTMVASofie not found.
Module PyMVA not found.
Module Genetic not found.
Module TMVAUtils not found.
Module Core not found.

```
