# Memory leak with basic TTree

**URL:** <https://root-forum.cern.ch/t/memory-leak-with-basic-ttree/50760>\
**Category:** ROOT\
**Created:** [July 11, 2022, 3:49pm UTC](https://root-forum.cern.ch/t/memory-leak-with-basic-ttree/50760 "2022-07-11T15:49:46Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [July 13, 2022, 1:09pm UTC](https://root-forum.cern.ch/t/memory-leak-with-basic-ttree/50760/5 "2022-07-13T13:09:58Z")

</div>

Hi @CorentinH,

thanks for following up and providing the full reproducer!

I think you have false positives from valgrind here. If I run your code in a look 1000 times and print the memory consumption, I can still see no leak:

```c++
#include <TSystem.h>
#include <TFile.h>
#include <TTree.h>

#include <iostream>
#include <stdlib.h>

struct Hit {
  UShort_t label = 0; //Hit label
  Float_t nrj = 0; //energy
  Int_t nrj2 = 0; //used if QDC2
  Int_t nrj3 = 0; //used if QDC3
  Int_t nrj4 = 0; //used if QDC4
  ULong64_t time = 0; //time
  Bool_t pileup = 0; //pile-up detection
  UInt_t coinc = 0; //Hit label
  UShort_t mult = 0; //Multiplicity of the hit
  Float_t nrjcal = 0;
};

void recreateFile() {
  Hit hit;
  srand48(time(0));
  TFile *file = new TFile("datafile.root", "recreate");
  TTree *tree = new TTree("DataTree", "DataTree");
  tree->Branch("nrj", &hit.nrj);
  tree->Branch("label", &hit.label);
  tree->Branch("time", &hit.time);
  for (int i = 0; i < 2000; i++) {
    hit.nrj = drand48() * 5;
    hit.time = drand48() * 5;
    hit.label = drand48() * 5;
    tree->Fill();
  }
  file->cd();
  tree->Write();
  file->Write();
  file->Close();
  delete file;
}

void repro() {
  ProcInfo_t pinfo;
  for (std::size_t i = 0; i < 10000; ++i) {
    recreateFile();

    if (i % 100 == 0) {
      gSystem->GetProcInfo(&pinfo);
      std::cout << i << " memory usage " << pinfo.fMemResident << " "
                << pinfo.fMemVirtual << std::endl;
    }
  }
}

```

Is there a way other than valgrind that you can observe the leak with? See also this write-up by Axel about why valgrind gives you false positives with ROOT:

> [@Valgrind and ROOT](https://root-forum.cern.ch/t/valgrind-and-root/28506):
>
> Memory Checks ROOT triggers a couple of [valgrind](http://valgrind.org/) reports that are intentional (see below if you care why). To suppress those use the following invocation: valgrind --num-callers=30 --suppressions=$ROOTSYS/etc/valgrind-root.supp root.exe -l -b -q myscript.C Note: yes, really root.exe, even on Linux and macOS! Threading Checks tbb (used by ROOT) is confusing helgrind; this is a [known issue](https://software.intel.com/en-us/forums/intel-threading-building-blocks/topic/295293). To suppress these known false-positives, please use valgrind --tool=helgrind --suppressions=$ROOTSYS/etc…

If you can’t get the suppression file from `$ROOTSYS`, you can also find it [here in the GitHub repository](https://github.com/root-project/root/blob/master/etc/valgrind-root.supp).

Hope this helps!  
Jonas

---

_[View the full topic](https://root-forum.cern.ch/t/memory-leak-with-basic-ttree/50760)._
