# The memory leak occured when transporting electrons and ions using AvalancheMC class

**URL:** <https://root-forum.cern.ch/t/the-memory-leak-occured-when-transporting-electrons-and-ions-using-avalanchemc-class/64316>\
**Category:** Garfield++\
**Created:** [October 15, 2025, 7:25am UTC](https://root-forum.cern.ch/t/the-memory-leak-occured-when-transporting-electrons-and-ions-using-avalanchemc-class/64316 "2025-10-15T07:25:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![newconcept1979](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/newconcept1979/32/26349_2.png) [@newconcept1979](https://root-forum.cern.ch/u/newconcept1979)\
**Post date:** [October 15, 2025, 7:25am UTC](https://root-forum.cern.ch/t/the-memory-leak-occured-when-transporting-electrons-and-ions-using-avalanchemc-class/64316/1 "2025-10-15T07:25:56Z")

</div>

I want to show the tracks of electrons and ions using AvalancheMC class. The key codes are:

```auto
AvalancheMC drift;
drift.SetSensor(&sensor);
drift.EnablePlotting(&driftView);
const unsigned int nTracks = 1;
  for (unsigned int j = 0; j < nTracks; ++j) {
    track.NewTrack(x0, y0, 0, 0, 0, 1, 0);
    std::cout<<"the total numbers of clusters is: "<<track.GetClusters().size()<<std::endl;
    for (const auto& cluster : track.GetClusters()) {
        int num_electrons=cluster.n;
        std::cout<<"the total electron numbers of evergy cluster is: "<<num_electrons<<std::endl;
        for(int jj=0;jj<num_electrons;jj++)
        {
            drift.DriftElectron(cluster.x,cluster.y,cluster.z,cluster.t);
            drift.DriftIon(cluster.x,cluster.y,cluster.z,cluster.t);
        }
    }
  }

```

when running the application, the memory leak occured.

by the way, the simple codes are:

```auto
AvalancheMC drift;
drift.SetSensor(&sensor);
drift.EnablePlotting(&driftView);
drift.DriftElectron(0.3,0.3,0,0);

```

with little probability, the codes worked; in most cases, the memory leak occured.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [October 15, 2025, 7:27am UTC](https://root-forum.cern.ch/t/the-memory-leak-occured-when-transporting-electrons-and-ions-using-avalanchemc-class/64316/2 "2025-10-15T07:27:42Z")

</div>

Thanks for reaching out! Let’s see what @hschindl think about this

---

<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:** [October 15, 2025, 7:41am UTC](https://root-forum.cern.ch/t/the-memory-leak-occured-when-transporting-electrons-and-ions-using-avalanchemc-class/64316/3 "2025-10-15T07:41:36Z")

</div>

Hi,  
how did you diagnose the memory leak? Just the memory usage going up? When you enable plotting, `ViewDrift` stores internally the coordinates of all points along the drift lines, so if you have many, many drift lines you may indeed have a significant memory consumption.

---

<div class="post-metadata">

**Author:** ![newconcept1979](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/newconcept1979/32/26349_2.png) [@newconcept1979](https://root-forum.cern.ch/u/newconcept1979)\
**Post date:** [October 15, 2025, 8:30am UTC](https://root-forum.cern.ch/t/the-memory-leak-occured-when-transporting-electrons-and-ions-using-avalanchemc-class/64316/4 "2025-10-15T08:30:18Z")

</div>

The command “top“ was used, and the memory usage went up quickly, at the end, the application was killed.

by the way, when using AvalancheMicroscopic class, the application run well, and the tracks of electrons were drawn:

[unnamed.pdf](https://root-forum.cern.ch/uploads/short-url/kGYMCGwdE7hXSb3Cq1FgAirwy0A.pdf) (1.5 MB)

---

<div class="post-metadata">

**Author:** ![newconcept1979](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/newconcept1979/32/26349_2.png) [@newconcept1979](https://root-forum.cern.ch/u/newconcept1979)\
**Post date:** [October 15, 2025, 8:52am UTC](https://root-forum.cern.ch/t/the-memory-leak-occured-when-transporting-electrons-and-ions-using-avalanchemc-class/64316/5 "2025-10-15T08:52:26Z")

</div>

by the way, when **commenting out the codes about** enable plotting:

```auto
// drift.EnablePlotting(&driftView);

```

the problem is still.

---

<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:** [October 15, 2025, 2:19pm UTC](https://root-forum.cern.ch/t/the-memory-leak-occured-when-transporting-electrons-and-ions-using-avalanchemc-class/64316/6 "2025-10-15T14:19:52Z")

</div>

Can you prepare a **minimal working example** that reproduces the issue?

---

<div class="post-metadata">

**Author:** ![newconcept1979](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/newconcept1979/32/26349_2.png) [@newconcept1979](https://root-forum.cern.ch/u/newconcept1979)\
**Post date:** [October 16, 2025, 12:23am UTC](https://root-forum.cern.ch/t/the-memory-leak-occured-when-transporting-electrons-and-ions-using-avalanchemc-class/64316/7 "2025-10-16T00:23:53Z")

</div>

here is the example.

[AvalancheMC.tar.gz](https://root-forum.cern.ch/uploads/short-url/2buGciXZAuGwSVg0oi1GjaHyfsF.gz) (128.0 KB)

---

<div class="post-metadata">

**Author:** ![newconcept1979](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/newconcept1979/32/26349_2.png) [@newconcept1979](https://root-forum.cern.ch/u/newconcept1979)\
**Post date:** [October 21, 2025, 7:28am UTC](https://root-forum.cern.ch/t/the-memory-leak-occured-when-transporting-electrons-and-ions-using-avalanchemc-class/64316/8 "2025-10-21T07:28:08Z")

</div>

@**[hschindl](https://root-forum.cern.ch/u/hschindl), with the example uploaded, does the memory leak take place in your machine?**

---

<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:** [October 25, 2025, 2:40pm UTC](https://root-forum.cern.ch/t/the-memory-leak-occured-when-transporting-electrons-and-ions-using-avalanchemc-class/64316/9 "2025-10-25T14:40:21Z")

</div>

Hi,  
sorry for the delay in getting back to you! No, I’m afraid I’ve not been able to reproduce the issue. I’ve used a slightly modified version of your example which I’ve attached. In your original example you had two places where you called `EnablePlotting`; which could explain why you saw an increase in memory usage with time even when you thought you had switched off plotting the drift lines.

When I switch off drift line visualisation (`plotDrift = false`), the memory consumption does not increase with the number of tracks simulated.

[test.C](https://root-forum.cern.ch/uploads/short-url/hEtSd2aqYWCZxHe4Agt4qqeOrTe.C) (2.5 KB)

PS:

- This isn’t directly related to you question, but I’ve also added a line (`SetDistanceSteps`) to set the target step size in `AvalancheMC` explicitly (to 0.1 mm, but this is something you might want to play with), because the default was giving strange results.
- Also, again not directly related to your question, but just to point out a potential misunderstanding: `AvalancheMC::DriftElectron` simulates the drift of a single electron but **does not** simulate the secondaries produced in the avalanche. This might not be what you want.

---

<div class="post-metadata">

**Author:** ![newconcept1979](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/newconcept1979/32/26349_2.png) [@newconcept1979](https://root-forum.cern.ch/u/newconcept1979)\
**Post date:** [October 26, 2025, 12:46am UTC](https://root-forum.cern.ch/t/the-memory-leak-occured-when-transporting-electrons-and-ions-using-avalanchemc-class/64316/10 "2025-10-26T00:46:12Z")

</div>

@hschindl, thanks for your patient reply to my question, when using the code in the file of test.C, the error occured with the code: Sensor sensor(&cmp); it seems no proper constructor function. and when using codes: Sensor sensor; sensor.AddComponent(&cmp); the application can run, and the memory consumption problem is still exist. so something is wrong with my Garfield problem. by the way, AvalancheMicroscopic::DriftElectron can produced in the avalanche?

---

<div class="post-metadata">

**Author:** ![newconcept1979](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/newconcept1979/32/26349_2.png) [@newconcept1979](https://root-forum.cern.ch/u/newconcept1979)\
**Post date:** [October 26, 2025, 8:10am UTC](https://root-forum.cern.ch/t/the-memory-leak-occured-when-transporting-electrons-and-ions-using-avalanchemc-class/64316/11 "2025-10-26T08:10:53Z")

</div>

@hschindl, after updating the program, everything works well.

---

<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:** [November 9, 2025, 8:11am UTC](https://root-forum.cern.ch/t/the-memory-leak-occured-when-transporting-electrons-and-ions-using-avalanchemc-class/64316/12 "2025-11-09T08:11:13Z")

</div>

Closed on November 9, 2025, 8:11am UTC

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