# Memory management with many TGraph.Fit()

**URL:** <https://root-forum.cern.ch/t/memory-management-with-many-tgraph-fit/43058>\
**Category:** Newbie\
**Tags:** python\
**Created:** [January 11, 2021, 4:08pm UTC](https://root-forum.cern.ch/t/memory-management-with-many-tgraph-fit/43058 "2021-01-11T16:08:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [January 11, 2021, 4:08pm UTC](https://root-forum.cern.ch/t/memory-management-with-many-tgraph-fit/43058/1 "2021-01-11T16:08:18Z")

</div>

I have a pseudocode which looks like this

```auto
double fitFunc(...){
    vector<double> x, y, x_err, y_err;
    // fill vectors
    if(x.size() <= 1) return 0;

    TGraphErrors gr(x.size(), &x[0], &y[0], &x_err[0], &y_err[0]);
    gr.Fit("pol1", "Q");
    TF1 fit = *gr.GetFunction("pol1");

    // Adding this part makes code `killed` after 100k events
    for(int j=0; j < n_refits; ++j){
        int idx = // pick some bad idx to throw away;
        x.erase(x.begin() + idx);
        // ...
        if(x.size() <= 1) return 0;

        gr = TGraphErrors(x.size(), &x[0], &y[0], &x_err[0], &y_err[0]);
        gr.Fit("pol1", "Q");
        fit = *gr.GetFunction("pol1");
    }
    return fit.GetParameter(0);
}

```

The function is called for each event inside with RDataFrame

Process gets `killed` after 100k events, which doesn’t happen with `n_refits = 0`.  
Because of that I think refit part of the code is very bad…

I am not good at mem leakages and this is probably basic C++ question  
Is there obvious mistake I am missing? Or how to improve this part of the code?

cheers

_ROOT Version:_ master  
_Platform:_ Centos 7  
_Compiler:_ gcc 10

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [January 12, 2021, 11:35am UTC](https://root-forum.cern.ch/t/memory-management-with-many-tgraph-fit/43058/2 "2021-01-12T11:35:02Z")

</div>

I can reproduce it, working on a fix for a future ROOT version. Until then, this should do as a workaround:

```auto
double fitFunc(...) {
    vector<double> x, y, x_err, y_err;
    // fill vectors
    if(x.size() <= 1) return 0;

    double ret = 0;
    TGraphErrors gr(x.size(), &x[0], &y[0], &x_err[0], &y_err[0]);
    gr.Fit("pol1", "Q");
    double ret = gr.GetFunction("pol1")->GetParameter(0);

    // Adding this part makes code `killed` after 100k events
    for(int j=0; j < n_refits; ++j){
        int idx = // pick some bad idx to throw away;
        x.erase(x.begin() + idx);
        // ...
        if(x.size() <= 1) return 0;

        TGraphErrors grRefit(x.size(), &x[0], &y[0], &x_err[0], &y_err[0]);
        grRefit.Fit("pol1", "Q");
        ret = grRefit.GetFunction("pol1")->GetParameter(0);
    }
    return ret;
}

```

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [January 12, 2021, 3:23pm UTC](https://root-forum.cern.ch/t/memory-management-with-many-tgraph-fit/43058/3 "2021-01-12T15:23:43Z")

</div>

It’s a leak in `fit = *gr.GetFunction("pol1");` (`TF1::Copy()` to be precise.) @moneta offered to provide a fix - thanks, Lorenzo!

@FoxWise thank you for reporting this!
