# Memory leak with RDataFrame in Python

**URL:** <https://root-forum.cern.ch/t/memory-leak-with-rdataframe-in-python/43069>\
**Category:** ROOT\
**Tags:** rdataframe\
**Created:** [January 12, 2021, 9:54am UTC](https://root-forum.cern.ch/t/memory-leak-with-rdataframe-in-python/43069 "2021-01-12T09:54:35Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [January 14, 2021, 11:14am UTC](https://root-forum.cern.ch/t/memory-leak-with-rdataframe-in-python/43069/4 "2021-01-14T11:14:45Z")

</div>

Hi,  
that script is a bit weird because as it loops it keeps adding more cuts, while I think the original intention was to use a different cut per iteration…?

Anyway with ROOT v6.22 this should help: instead of

```python
	cuts = {}
	for i in range(0,100):
		cuts['lePt%s'%i] = 'lep_0_p4.Pt()>%s'%i
		hist = loopDataFrame( treeName, file, cuts )
		hist.Draw()

```

book all computations first, use the results second:

```python
    cuts = {}
    histos = []
	for i in range(0,100):
		cuts['lePt%s'%i] = 'lep_0_p4.Pt()>%s'%i
		hist = loopDataFrame( treeName, file, cuts )
		histos.append(hist)
   for h in histos:
      h.GetValue() # or Draw, or whatever

```

For the multiproc solution, the idea is to run each `loopDataFrame` invocation in a different subrocess using e.g. [a process pool](https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool), so when the processing of a dataframe is done the related worker process is killed and the memory allocated by the interpreter is freed with it.

These might be good suggestions for your usecase or not, I’d need to see a reproducer to check what exactly is hogging memory in your case.

Hope this helps!  
Enrico

---

_[View the full topic](https://root-forum.cern.ch/t/memory-leak-with-rdataframe-in-python/43069)._
