# Memory leak when processing RDataFrames in python loop

**URL:** <https://root-forum.cern.ch/t/memory-leak-when-processing-rdataframes-in-python-loop/51170>\
**Category:** ROOT\
**Tags:** rdataframe\
**Created:** [August 12, 2022, 1:51pm UTC](https://root-forum.cern.ch/t/memory-leak-when-processing-rdataframes-in-python-loop/51170 "2022-08-12T13:51:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![asopio](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/asopio/32/39632_2.png) [@asopio](https://root-forum.cern.ch/u/asopio)\
**Post date:** [August 12, 2022, 1:51pm UTC](https://root-forum.cern.ch/t/memory-leak-when-processing-rdataframes-in-python-loop/51170/1 "2022-08-12T13:51:23Z")

</div>

Dear ROOT experts,

I’ve run into some weird behaviour while trying to read a series of TTrees from separate files and process them using RDataFrames inside a python loop. Here is some example code that reproduces the error:

```auto
mems = []
for i in range(100):
    mems.append( psutil.Process().memory_info().rss / 1024**2 )

    f = ROOT.TFile(infile_name)
    tch = f.Get('nominal')
    
    rdf = ROOT.RDataFrame(tch)
    rdf = rdf.Filter('eventNumber>367927315')

    ens = rdf.AsNumpy(['eventNumber'])
    print(np.sum(ens['eventNumber']))
    
    f.Close()

```

As you can see, I’m trying to read trees inside the loop, apply a filter using an RDataFrame and then do some procesing in numpy. However, even though the file is closed and the numpy arrays go out of scope and are collected by the python garbage collector, the amount of memory used keeps growing:

![image](https://root-forum.cern.ch/uploads/default/original/3X/7/a/7aab5b696511cb9294cd8f73c36334cc43641957.png)

Curiously, if I get rid of the Filter, the memory usage is much more limited:

![image](https://root-forum.cern.ch/uploads/default/original/3X/6/7/671f389d8c09cf0523ef1fb8e5a29ed3b8e95d9b.png)

What is actually going on here? Is there a way to avoid this?

Cheers,  
Alex

* * *

_Please read [tips for efficient and successful posting](https://root-forum.cern.ch/t/tips-for-efficient-and-successful-posting/28292) and [posting code](https://root-forum.cern.ch/t/posting-code-read-this-first/28293)_

_ROOT Version:_ 6.18/04  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

---

<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:** [August 12, 2022, 3:15pm UTC](https://root-forum.cern.ch/t/memory-leak-when-processing-rdataframes-in-python-loop/51170/2 "2022-08-12T15:15:52Z")

</div>

Not sure why. Maybe try this:

```auto
    rdf0 = ROOT.RDataFrame(tch)
    rdf = rdf0.Filter(...)

```

---

<div class="post-metadata">

**Author:** ![asopio](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/asopio/32/39632_2.png) [@asopio](https://root-forum.cern.ch/u/asopio)\
**Post date:** [August 12, 2022, 3:46pm UTC](https://root-forum.cern.ch/t/memory-leak-when-processing-rdataframes-in-python-loop/51170/3 "2022-08-12T15:46:14Z")

</div>

Hi ferhue,

Unfortunately, this does not change anything. Even when I explicitly delete the RDataFrame the memory used keeps growing.

Cheers,  
Alex

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 12, 2022, 5:52pm UTC](https://root-forum.cern.ch/t/memory-leak-when-processing-rdataframes-in-python-loop/51170/4 "2022-08-12T17:52:27Z")

</div>

Hi @asopio ,

without a complete minimal reproducer that I can run, my guess is that the memory hogging comes from the code for the `Filter` expression that (in ROOT v6.18) is re-compiled at every loop (see e.g. [How to delete RDataFrame and clean up memory - #2 by eguiraud](https://root-forum.cern.ch/t/how-to-delete-rdataframe-and-clean-up-memory/33036/2) ). Code that is just-in-time-compiled stays in memory until the end of the application. The amount of code generated at every new loop is much much smaller in more recent ROOT versions. Can you please try with ROOT v6.26.06? There should be a little increase, but nothing as dramatic as ~4MB per iteration.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![asopio](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/asopio/32/39632_2.png) [@asopio](https://root-forum.cern.ch/u/asopio)\
**Post date:** [August 15, 2022, 12:12pm UTC](https://root-forum.cern.ch/t/memory-leak-when-processing-rdataframes-in-python-loop/51170/5 "2022-08-15T12:12:11Z")

</div>

Hi Enrico,  
Thanks for the quick response!

I ran the code again in v6.26 and the rate of memory growth is a lot lower than it was before:

v6.18:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/1/e/1e7622de8216220928e1c1a9703f96d5d8235a01.png)

v6.26:

 ![memgraph_v6.26](https://root-forum.cern.ch/uploads/default/original/3X/f/3/f3610574f653e757584a8d7963b0021b13bc8077.png)

However, I’m still confused as to why there is a steady increase in memory usage at all. Is it really only just-in-time-compiled code? Is there a good reason for why it is kept in memory even after the RDataFrame object gets deleted?

Here is a full minimal reproducer for the plots above:  
[dataframetest.py](https://root-forum.cern.ch/uploads/short-url/hRLjMuNpG3exbCami2N3NM43LCr.py) (918 Bytes)

Cheers,  
Alex

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 15, 2022, 12:57pm UTC](https://root-forum.cern.ch/t/memory-leak-when-processing-rdataframes-in-python-loop/51170/6 "2022-08-15T12:57:15Z")

</div>

Hi @asopio ,

> [@asopio](#):
>
> the rate of memory growth is a lot lower than it was before

Great! We did something right between releases 🙂

> [@asopio](#):
>
> Is it really only just-in-time-compiled code?

Yes, it’s easy to verify with `valgrind --tool=massif`. These are the largest memory hoggers it sees (thank you for the self-contained reproducer):

```auto
12.15% (30,056,448B) 0x9E43CA4: llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, 4096ul, 4096ul>::Allocate(unsigned long, unsigned long) [clone .constprop.0] (in /home/blue/ROOT/master/cmake-build-foo/lib/libCling.so)
06.47% (15,994,384B) 0x7DBB588: clang::ASTReader::ReadASTBlock(clang::serialization::ModuleFile&, unsigned int) (in /home/blue/ROOT/master/cmake-build-foo/lib/libCling.so)
06.41% (15,848,928B) 0x7DBBAE5: clang::ASTReader::ReadASTBlock(clang::serialization::ModuleFile&, unsigned int) (in /home/blue/ROOT/master/cmake-build-foo/lib/libCling.so)
05.33% (13,189,120B) 0x9D093D8: llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, 4096ul, 4096ul>::Allocate(unsigned long, unsigned long) [clone .constprop.1] (in /home/blue/ROOT/master/cmake-build-foo/lib/libCling.so)
05.21% (12,875,749B) 0xA4005F8: llvm::Module::getOrInsertComdat(llvm::StringRef) (in /home/blue/ROOT/master/cmake-build-foo/lib/libCling.so)
05.07% (12,549,184B) 0x7160CC2: llvm::safe_malloc(unsigned long) (MemAlloc.h:26)
03.83% (9,461,760B) 0x9D16DD8: llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, 4096ul, 4096ul>::Allocate(unsigned long, unsigned long) [clone .constprop.0] (in /home/blue/ROOT/master/cmake-build-foo/lib/libCling.so)
03.39% (8,380,856B) 0xA4FA33A: llvm::SmallVectorBase::grow_pod(void*, unsigned long, unsigned long) (in /home/blue/ROOT/master/cmake-build-foo/lib/libCling.so)
03.25% (8,032,344B) 0xA4EF7E1: llvm::WritableMemoryBuffer::getNewUninitMemBuffer(unsigned long, llvm::Twine const&) (in /home/blue/ROOT/master/cmake-build-foo/lib/libCling.so)

```

etc. etc. (the percentages indicate the fraction of the memory allocated by the process that was allocated by that particular call, the biggest offender is `llvm::BumpPtrAllocatorImpl` at 12%).

> [@asopio](#):
>
> Is there a good reason for why it is kept in memory even after the RDataFrame object gets deleted?

Just-in-time-compiled code is added to an in-memory “shared library”, and Cling, the interpreter, provides no way to unload code once it’s there. On the RDataFrame side we try to maximize jitted code re-use.

### Further mitigations

Besides using the latest ROOT release which is a bit more well-behaved, you can use, instead of `rdf = rdf.Filter("eventNumber>367927315")`, something like:

```python
ROOT.gInterpreter.Declare("""
ROOT::RDF::RNode ApplyFilter(ROOT::RDF::RNode df) {
    return df.Filter([](int e) { return e>367927315; }, {"eventNumber"});
}
""")
...
rdf = ROOT.ApplyFilter(ROOT.RDF.AsRNode(rdf))

```

With that trick we just-in-time-compile the RDF transformation you need only once, then re-use it many times (note that we need to use a C++ lambda inside the `Filter` there otherwise we are back to the original situation). On my machine this is the situation with your original reproducer:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/2/e/2eada859e29151d867ee4d69156117bfb7508d16.png)

and this is with the change mentioned above:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/b/b/bb897492dae31870e3eb1c17df103345465062ae.png)

There is still a little bit of memory creep because `AsNumpy` does a little bit of jitting as well.

The other thing you can do is run each iteration of your loop in a sub-process. When the sub-process ends, it deallocates all related memory.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![asopio](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/asopio/32/39632_2.png) [@asopio](https://root-forum.cern.ch/u/asopio)\
**Post date:** [August 15, 2022, 1:23pm UTC](https://root-forum.cern.ch/t/memory-leak-when-processing-rdataframes-in-python-loop/51170/7 "2022-08-15T13:23:53Z")

</div>

Hi Enrico,

Thank you very much for this in-depth explanation!

Cheers,  
Alex

---

<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:** [August 29, 2022, 1:24pm UTC](https://root-forum.cern.ch/t/memory-leak-when-processing-rdataframes-in-python-loop/51170/8 "2022-08-29T13:24:35Z")

</div>

Closed on August 29, 2022, 1:24pm UTC

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