# pyROOT and std.vector - dramatic RAM usage

**URL:** <https://root-forum.cern.ch/t/pyroot-and-std-vector-dramatic-ram-usage/49711>\
**Category:** ROOT\
**Created:** [April 23, 2022, 11:52am UTC](https://root-forum.cern.ch/t/pyroot-and-std-vector-dramatic-ram-usage/49711 "2022-04-23T11:52:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpudzha](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dpudzha/32/29475_2.png) [@dpudzha](https://root-forum.cern.ch/u/dpudzha)\
**Post date:** [April 23, 2022, 11:52am UTC](https://root-forum.cern.ch/t/pyroot-and-std-vector-dramatic-ram-usage/49711/1 "2022-04-23T11:52:06Z")

</div>

Hello everyone.

I’m looping a tree with pyROOT and I need to rewrite content to another tree with modifications. I want to have vectors of ints as branches and for that I’m using  
ROOT.std.vector(ROOT.int)()  
But this dramatically increase my ram memory use. Even If I just declare the vector and I never used it - memory usage is just getting bigger while I’m looping trough the tree but if I don’t declare that - it’s look fine. What could be the problem here?

I attached the files to the topic. There you have test.py is a main file, Hit.py - is a class I want to have and a file with tree. Inside test.py a line 17 which is giving troubles. I also added function getMem() to see memory usage.  
[cernbox.cern.ch/index.php/s/yPbW2j5EQsRkZE4](https://cernbox.cern.ch/index.php/s/yPbW2j5EQsRkZE4)

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 25, 2022, 1:56pm UTC](https://root-forum.cern.ch/t/pyroot-and-std-vector-dramatic-ram-usage/49711/2 "2022-04-25T13:56:44Z")

</div>

Hello,

Ok thanks for reporting, I will have a look.

Just a quick question before looking into it, you iterate on the tree but not on the vector itself? I’m asking because we recently fixed a memory leak when iterating on STL collections:

> <https://github.com/root-project/root/issues/9315>
>
> Reproducer taken from the forum (https://root-forum.cern.ch/t/memory-leak-using-…root-std-map-int-t-float-t/47737):
> 
> \`\`\`python
> import ROOT
> allChannels = ROOT.std.map(’\<Int\_t,Float\_t\>’)()
> allChannels\[3\]=5.
> N=0
> for n in range(10000000):
> for c in allChannels:
> N+=1
> \`\`\`

---

<div class="post-metadata">

**Author:** ![dpudzha](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dpudzha/32/29475_2.png) [@dpudzha](https://root-forum.cern.ch/u/dpudzha)\
**Post date:** [April 25, 2022, 2:24pm UTC](https://root-forum.cern.ch/t/pyroot-and-std-vector-dramatic-ram-usage/49711/3 "2022-04-25T14:24:06Z")

</div>

Hello,

Yes, I just declared a vector but never used it. I’m looping over a tree and it makes very big difference if it’s being declared or not.

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 25, 2022, 2:33pm UTC](https://root-forum.cern.ch/t/pyroot-and-std-vector-dramatic-ram-usage/49711/4 "2022-04-25T14:33:22Z")

</div>

Thanks, I was able to reproduce, I will investigate and get back to you!

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 25, 2022, 2:55pm UTC](https://root-forum.cern.ch/t/pyroot-and-std-vector-dramatic-ram-usage/49711/5 "2022-04-25T14:55:43Z")

</div>

Interestingly enough, it has to be a vector of ints (does not happen e.g. with vector of string or float).

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 26, 2022, 10:10am UTC](https://root-forum.cern.ch/t/pyroot-and-std-vector-dramatic-ram-usage/49711/6 "2022-04-26T10:10:48Z")

</div>

I don’t know the cause yet, but I found a workaround (in case it can be useful for you).

The problem comes down to instantiating an std::vector before the “layer” branch is read (which is also of type std::vector).

If you make sure you read at least once the branch before you instantiate the vector, that should be enough. Were you creating a vector to read the branch into that vector? If so, from Python you don’t need to do that, just calling `tree.branchname` gives you the branch for that particular event in a loop like this:

```python
for event in tree:
    # l should be a vector<int>
    l = event.layer

```

---

<div class="post-metadata">

**Author:** ![dpudzha](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dpudzha/32/29475_2.png) [@dpudzha](https://root-forum.cern.ch/u/dpudzha)\
**Post date:** [April 26, 2022, 10:29am UTC](https://root-forum.cern.ch/t/pyroot-and-std-vector-dramatic-ram-usage/49711/7 "2022-04-26T10:29:11Z")

</div>

Thank you! For now I switched to ROOT.RDataFrame so now I don’t need to loop over the tree which was causing the increase over time.

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 26, 2022, 10:49am UTC](https://root-forum.cern.ch/t/pyroot-and-std-vector-dramatic-ram-usage/49711/8 "2022-04-26T10:49:01Z")

</div>

Very good decision to switch to RDF in any case!

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 26, 2022, 3:13pm UTC](https://root-forum.cern.ch/t/pyroot-and-std-vector-dramatic-ram-usage/49711/9 "2022-04-26T15:13:21Z")

</div>

After some debugging, I opened this issue with an explanation of the issue and a reproducer in C++:

> <https://github.com/root-project/root/issues/10454>
>
> Initially reported here:
> 
> https://root-forum.cern.ch/t/pyroot-and-std-vector-d…ramatic-ram-usage/49711
> 
> when a user was getting a \`vector\<int\>\` branch from a tree in a loop in PyROOT.
> 
> After a bit of digging, it seems the culprit is a call that is made from cppyy to \`TInterpreter::ClassInfo\_IsEnum\`. The following is a C++ reproducer (tested with current master):
> 
> \`\`\`python
> float getMem() {
> auto info = ProcInfo\_t();
> gSystem-\>GetProcInfo(&info);
> float mem = (float)info.fMemResident;
> return mem\*1e-3;
> }
> 
> void test()
> {
> for (int i=0; i \< 100000; ++i) {
> gInterpreter-\>ClassInfo\_IsEnum("vector\<int\>");
> if (i%1000 == 0)
> printf("RSS at iteration %d is %f\\n", i, getMem());
> }
> }
> \`\`\`
> 
> When running the macro above, it can be seen how the memory usage increases after each iteration.

I suggest we follow it there!

---

<div class="post-metadata">

**Author:** ![dpudzha](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dpudzha/32/29475_2.png) [@dpudzha](https://root-forum.cern.ch/u/dpudzha)\
**Post date:** [April 27, 2022, 5:03am UTC](https://root-forum.cern.ch/t/pyroot-and-std-vector-dramatic-ram-usage/49711/10 "2022-04-27T05:03:37Z")

</div>

Thank you for that, I will follow it there!

---

<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:** [May 11, 2022, 5:04am UTC](https://root-forum.cern.ch/t/pyroot-and-std-vector-dramatic-ram-usage/49711/11 "2022-05-11T05:04:15Z")

</div>

Closed on May 11, 2022, 5:04am UTC

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