# Memory leak of Garfield++?

**URL:** <https://root-forum.cern.ch/t/memory-leak-of-garfield/50624>\
**Category:** Garfield++\
**Created:** [July 1, 2022, 4:16am UTC](https://root-forum.cern.ch/t/memory-leak-of-garfield/50624 "2022-07-01T04:16:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [July 1, 2022, 4:16am UTC](https://root-forum.cern.ch/t/memory-leak-of-garfield/50624/1 "2022-07-01T04:16:31Z")

</div>

Dear experts

I simulated ~10k tracks using Heed+ and drift using AvalancheMC, but the job was killed on server because of insufficient memory.

Then I tried to run the **DriftTube** example on my local PC to monitor memory usage, I found it may have memory leak:  
I tried 3 track (original number written in the code) it cost ~180Mb memory,  
then I tried 1k track, it cost ~480Mb and was linearly increasing when it’s running.  
So if I try like 100k I believe my PC will be not able to afford the memory requirement…  
(I disabled plotting when simulate large number of tracks)  
Maybe new track caused memory leak or I misunderstood something?

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [July 1, 2022, 4:32am UTC](https://root-forum.cern.ch/t/memory-leak-of-garfield/50624/2 "2022-07-01T04:32:13Z")

</div>

Thanks for reporting this! Unfortunately I’m not able to reproduce it. Are you 100% sure you disabled plotting? If you don’t (and if you don’t clear the plot once in a while), `ViewDrift` will keep accumulating drift lines and the memory consumption will indeed grow with every track that you simulate.

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [July 1, 2022, 4:57am UTC](https://root-forum.cern.ch/t/memory-leak-of-garfield/50624/3 "2022-07-01T04:57:55Z")

</div>

Here is what exactly I ran:

```auto
//I didn't show codes before since I didn't modify
const double rTrack = 0.3;
  const double x0 = rTrack;
  const double y0 = -sqrt(rTube * rTube - rTrack * rTrack);
  const unsigned int nTracks = 10000;
  for (unsigned int j = 0; j < nTracks; ++j) {
    sensor.ClearSignal();
    track.NewTrack(x0, y0, 0, 0, 0, 1, 0);
    std::cout<<"test"<<std::endl;
    double xc = 0., yc = 0., zc = 0., tc = 0., ec = 0., extra = 0.;
    int nc = 0;
    while (track.GetCluster(xc, yc, zc, tc, nc, ec, extra)) {
      for (int k = 0; k < nc; ++k) {
        double xe = 0., ye = 0., ze = 0., te = 0., ee = 0.;
        double dx = 0., dy = 0., dz = 0.;
        track.GetElectron(k, xe, ye, ze, te, ee, dx, dy, dz);
        drift.DriftElectron(xe, ye, ze, te);
      }
    }
    std::cout<<j<<" finished"<<std::endl;
/*    
    if (plotDrift) {
      cD->Clear();
      cellView.Plot2d();
      constexpr bool twod = true;
      constexpr bool drawaxis = false;
      driftView.Plot(twod, drawaxis);
    }
    sensor.ConvoluteSignals();
    int nt = 0;
    if (!sensor.ComputeThresholdCrossings(-2., "s", nt)) continue;
    if (plotSignal) signalView.PlotSignal("s");
*/   
  }

```

I tried 10k this time to see the linear increasing of memory usage, and it does increased linearly.

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [July 1, 2022, 5:01am UTC](https://root-forum.cern.ch/t/memory-leak-of-garfield/50624/4 "2022-07-01T05:01:38Z")

</div>

If you didn’t change this part of the code:

```auto
constexpr bool plotDrift = true;
if (plotDrift) {
  cD = new TCanvas("cD", "", 600, 600);
  cellView.SetCanvas(cD);
  cellView.SetComponent(&cmp);
  driftView.SetCanvas(cD);
  drift.EnablePlotting(&driftView);
  track.EnablePlotting(&driftView);
}

```

then `driftView` will accumulate all the drift lines you simulate and so your memory usage will increase.

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [July 1, 2022, 5:08am UTC](https://root-forum.cern.ch/t/memory-leak-of-garfield/50624/5 "2022-07-01T05:08:11Z")

</div>

You are right, I just disabled plotting (action) but didn’t disabled drift line calculations, now I understood, thank you!

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [July 1, 2022, 5:19am UTC](https://root-forum.cern.ch/t/memory-leak-of-garfield/50624/7 "2022-07-01T05:19:56Z")

</div>

Just to be precise: you just need to set the flag `plotDrift` to `false`. You still need to simulate the drift lines of course if you want to calculate the induced signal.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [July 15, 2022, 5:20am UTC](https://root-forum.cern.ch/t/memory-leak-of-garfield/50624/8 "2022-07-15T05:20:30Z")

</div>

Closed on July 15, 2022, 5:20am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
