# Memory leak during repeated fitting

**URL:** <https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299>\
**Category:** ROOT\
**Tags:** roofit, root\
**Created:** [February 26, 2024, 1:46pm UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299 "2024-02-26T13:46:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![VictorBaryshnikov](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/b4bc9f/32.png) [@VictorBaryshnikov](https://root-forum.cern.ch/u/VictorBaryshnikov)\
**Post date:** [February 26, 2024, 1:46pm UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/1 "2024-02-26T13:46:48Z")

</div>

Memory leak during repeated fitting. See macro.

ROOT Version: 6.28/10  
Platform: Ubuntu 20.04  
[example\_fit.cpp](https://root-forum.cern.ch/uploads/short-url/l8IqnGWPD25dxUYrhu07AYT8VEo.cpp) (3.1 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 26, 2024, 1:49pm UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/2 "2024-02-26T13:49:47Z")

</div>

Welcome to the ROOT forum.

I think your post is missing the “macros”.

---

<div class="post-metadata">

**Author:** ![VictorBaryshnikov](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/b4bc9f/32.png) [@VictorBaryshnikov](https://root-forum.cern.ch/u/VictorBaryshnikov)\
**Post date:** [February 26, 2024, 1:53pm UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/3 "2024-02-26T13:53:21Z")

</div>

[example\_fit.cpp](https://root-forum.cern.ch/uploads/short-url/l8IqnGWPD25dxUYrhu07AYT8VEo.cpp) (3.1 KB)

---

<div class="post-metadata">

**Author:** ![VictorBaryshnikov](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/b4bc9f/32.png) [@VictorBaryshnikov](https://root-forum.cern.ch/u/VictorBaryshnikov)\
**Post date:** [February 26, 2024, 1:53pm UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/4 "2024-02-26T13:53:41Z")

</div>

Attached

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 27, 2024, 6:45am UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/5 "2024-02-27T06:45:19Z")

</div>

```auto
Processing example_fit.cpp...
In file included from input_line_9:1:
/Users/couet/Downloads/example_fit.cpp:33:10: fatal error: 'MpdADC64ECalDigit.h' file not found
#include "MpdADC64ECalDigit.h"
         ^ ~~~~~~~~~~~~~~~~~~~~

```

---

<div class="post-metadata">

**Author:** ![VictorBaryshnikov](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/b4bc9f/32.png) [@VictorBaryshnikov](https://root-forum.cern.ch/u/VictorBaryshnikov)\
**Post date:** [February 27, 2024, 7:26am UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/6 "2024-02-27T07:26:08Z")

</div>

This library is not needed for this example and it can be safely commented out or deleted.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 27, 2024, 7:45am UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/7 "2024-02-27T07:45:18Z")

</div>

I ran you macro several times in the same ROOT session. Each time I execute it I see the memory size used by root.exe increasing by 2 MB. is that what you see also ?

---

<div class="post-metadata">

**Author:** ![VictorBaryshnikov](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/b4bc9f/32.png) [@VictorBaryshnikov](https://root-forum.cern.ch/u/VictorBaryshnikov)\
**Post date:** [February 27, 2024, 7:51am UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/8 "2024-02-27T07:51:52Z")

</div>

I run a macro and look at the amount of resources that this process consumes. As you can see from at least two slides, the amount of memory is increasing (constantly).

 ![Screenshot from 2024-02-27 10-47-50](https://root-forum.cern.ch/uploads/default/original/3X/3/e/3e1034951ebfc9201b87fb8fa24bbbf4951377da.png)  
 ![Screenshot from 2024-02-27 10-47-44](https://root-forum.cern.ch/uploads/default/original/3X/4/0/406282453b4a5272275b371d22d6e007449861c4.png)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 27, 2024, 7:53am UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/9 "2024-02-27T07:53:19Z")

</div>

Yes that’s what I mean

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 27, 2024, 8:00am UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/10 "2024-02-27T08:00:12Z")

</div>

I see it also increasing with this simple one:

```auto
void FitOscillograms()
{
    vector<Double_t> data {12, 0, 15, -2, 6, -2, 6, 0, -14, -11, 22, -3, 21, 3, -22, -651, -2131, -3031, -3323, -2923, -2579, -2267, -1899, -1603, -1299, -1075, -875, -719, -563, -459, -347, -299, -219, -195, -147, -143, -103, -91, -48, -50, -23, -10, 3, 2, 16, 10, 32, 14, 17, -10, 6, -3, 4, 3, 17, 8, 7, -1, 13, 3};

    for (int j = 0; j < 60000 ; j++)
    {
        Double_t MaxDelta = 3323;
        Double_t BinMax = 19;
    }
}

void example_fit()
{
    FitOscillograms();
}

```

---

<div class="post-metadata">

**Author:** ![VictorBaryshnikov](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/b4bc9f/32.png) [@VictorBaryshnikov](https://root-forum.cern.ch/u/VictorBaryshnikov)\
**Post date:** [February 27, 2024, 8:10am UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/11 "2024-02-27T08:10:31Z")

</div>

It is clear how to deal with this.

Here are two different codes:

1. graph-\>Fit()… - there is a memory leak  
void FitOscillograms()  
{  
vector\<Double\_t\> data {12, 0, 15, -2, 6, -2, 6, 0, -14, -11, 22, -3, 21, 3, -22, -651, -2131, -3031, -3323, -2923, -2579, -2267, -1899, -1603, -1299, -1075, -875, -719, -563, -459, -347, -299, -219, -195, -147, -143, -103, -91, -48, -50, -23, -10, 3, 2, 16, 10, 32, 14, 17, -10, 6, -3, 4, 3, 17, 8, 7, -1, 13, 3};  
Double\_t MaxDelta = 3323;  
Double\_t BinMax = 19;

2. // graph-\>Fit()… - no memory leak  
void FitOscillograms()  
{  
vector\<Double\_t\> data {12, 0, 15, -2, 6, -2, 6, 0, -14, -11, 22, -3, 21, 3, -22, -651, -2131, -3031, -3323, -2923, -2579, -2267, -1899, -1603, -1299, -1075, -875, -719, -563, -459, -347, -299, -219, -195, -147, -143, -103, -91, -48, -50, -23, -10, 3, 2, 16, 10, 32, 14, 17, -10, 6, -3, 4, 3, 17, 8, 7, -1, 13, 3};  
Double\_t MaxDelta = 3323;  
Double\_t BinMax = 19;

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 27, 2024, 8:23am UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/12 "2024-02-27T08:23:55Z")

</div>

It seems you are claiming the memory size increases while running the macro ? right ? not when you are executing the macro several times ? In that case I do not see any memory leak with:

```auto
Double_t Novosibirsk(Double_t* x, Double_t* p)
{
   //As Defined in RooNovosibirsk.cxx
   //If tail=eta=0 the Belle distribution becomes gaussia
   double tail = p[1];
   double width = p[2];
   double peak = p[3];
   if (TMath::Abs(tail) < 1.e-7)
     return p[0] * TMath::Exp(-0.5 * TMath::Power(((x[0] - peak) / width), 2));
   Double_t arg = 1.0 - (x[0] - peak) * tail / width;
   if (arg < 1.e-6) return 0.0; //Argument of logaritem negative. Real continuation -> function equals zero
   
   Double_t log = TMath::Log(arg);
   static const Double_t xi = 2.3548200450309494; // 2 Sqrt( Ln(4) )
   Double_t width_zero = (2.0 / xi) * TMath::ASinH(tail * xi * 0.5);
   Double_t width_zero2 = width_zero * width_zero;
   Double_t exponent = (-0.5 / (width_zero2) * log * log) - (width_zero2 * 0.5);
   
   return p[0] * TMath::Exp(exponent);
}

void FitOscillograms()
{
   vector<Double_t> data {12, 0, 15, -2, 6, -2, 6, 0, -14, -11, 22, -3, 21, 3, -22, -651, -2131, -3031, -3323, -2923, -2579, -2267, -1899, -1603, -1299, -1075, -875, -719, -563, -459, -347, -299, -219, -195, -147, -143, -103, -91, -48, -50, -23, -10, 3, 2, 16, 10, 32, 14, 17, -10, 6, -3, 4, 3, 17, 8, 7, -1, 13, 3};
   Double_t MaxDelta = 3323;
   Double_t BinMax = 19;
   
   TGraphErrors* graph = new TGraphErrors(60);
   
   for(int i = 0; i < 60; i++) {
      graph->SetPoint (i, i, 0 - data[i]);
      graph->SetPointError(i, 0, 5);
   }
   
   TF1* pFit = new TF1("Novosibirsk", Novosibirsk, 0., 60, 4);
   pFit->SetParameters(MaxDelta, 0.05, 8., BinMax);
   
   for (int j = 0; j < 60000000 ; j++)
      graph->Fit(pFit, "QR0", "");
   }
   
   delete graph;
   delete pFit;
}

```

it is stable at 77.5 MB

---

<div class="post-metadata">

**Author:** ![VictorBaryshnikov](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/b4bc9f/32.png) [@VictorBaryshnikov](https://root-forum.cern.ch/u/VictorBaryshnikov)\
**Post date:** [February 27, 2024, 9:02am UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/13 "2024-02-27T09:02:28Z")

</div>

Yes, I claim that the amount of memory increases during the execution of the macro. That’s what surprises me. For reliability, I used the code that you attached and attached a screenshot with the archived video.

 ![Screenshot from 2024-02-27 12-01-08](https://root-forum.cern.ch/uploads/default/original/3X/d/9/d9b7211bfd675e36b7cb88cb084fd94ee477b42c.png)  
[Example.zip](https://root-forum.cern.ch/uploads/short-url/hyo38dXFoaN3cSyVRzYwZmAW4mO.zip) (47.2 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 27, 2024, 9:15am UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/14 "2024-02-27T09:15:03Z")

</div>

As I said the last example I ran on my Mac shows a stable memory usage. As it is related to fitting may be @moneta might have an idea about it.

---

<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:** [February 27, 2024, 10:25am UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/15 "2024-02-27T10:25:50Z")

</div>

I also see stable memory usage with 6.30.04 on Ubuntu 22.04.

I suggest you try running it with valgrind.

---

<div class="post-metadata">

**Author:** ![VictorBaryshnikov](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/b4bc9f/32.png) [@VictorBaryshnikov](https://root-forum.cern.ch/u/VictorBaryshnikov)\
**Post date:** [February 27, 2024, 10:43am UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/16 "2024-02-27T10:43:15Z")

</div>

==355611== HEAP SUMMARY:  
==355611== in use at exit: 128,157,684 bytes in 200,175 blocks  
==355611== total heap usage: 7,128,345 allocs, 6,928,170 frees, 1,679,837,281 bytes allocated  
==355611==  
==355611== LEAK SUMMARY:  
==355611== definitely lost: 20,149,304 bytes in 179,900 blocks  
==355611== indirectly lost: 0 bytes in 0 blocks  
==355611== possibly lost: 51,480 bytes in 770 blocks  
==355611== still reachable: 107,956,900 bytes in 19,505 blocks  
==355611== of which reachable via heuristic:  
==355611== newarray : 1,208 bytes in 1 blocks  
==355611== multipleinheritance: 3,552 bytes in 6 blocks  
==355611== suppressed: 0 bytes in 0 blocks  
==355611== Rerun with --leak-check=full to see details of leaked memory  
==355611==  
==355611== Use --track-origins=yes to see where uninitialised values come from  
==355611== For lists of detected and suppressed errors, rerun with: -s  
==355611== ERROR SUMMARY: 298 errors from 53 contexts (suppressed: 0 from 0)  
[example.txt](https://root-forum.cern.ch/uploads/short-url/ygKGmTVSj6BwzAPlLBc5MbwUNc7.txt) (100.1 KB)

---

<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:** [February 27, 2024, 11:59am UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/17 "2024-02-27T11:59:47Z")

</div>

Mm can you repost, but running it with: --leak-check=full --suppressions=/opt/root\_src/etc/valgrind-root.supp

---

<div class="post-metadata">

**Author:** ![VictorBaryshnikov](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/b4bc9f/32.png) [@VictorBaryshnikov](https://root-forum.cern.ch/u/VictorBaryshnikov)\
**Post date:** [February 27, 2024, 12:38pm UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/18 "2024-02-27T12:38:40Z")

</div>

[example1.txt](https://root-forum.cern.ch/uploads/short-url/1oKZ6BVoU56natEYejYUQxJTqk8.txt) (37.6 KB)

---

<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:** [February 27, 2024, 12:56pm UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/19 "2024-02-27T12:56:56Z")

</div>

It seems that this leak is the same as in: [Valgrind reports leak when constructing TChain · Issue #13130 · root-project/root · GitHub](https://github.com/root-project/root/issues/13130)

Can you try with ROOT 6.30.04 instead? I think the memory leak was fixed in the meanwhile.

---

<div class="post-metadata">

**Author:** ![VictorBaryshnikov](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/b4bc9f/32.png) [@VictorBaryshnikov](https://root-forum.cern.ch/u/VictorBaryshnikov)\
**Post date:** [February 27, 2024, 2:37pm UTC](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299/20 "2024-02-27T14:37:34Z")

</div>

Yes, you were right. The problem was this, there is no memory leak in the root of the latest version when executing this macro.

Thanks!

[Next page](https://root-forum.cern.ch/t/memory-leak-during-repeated-fitting/58299.md?page=2)
