# How to delete RDataFrame and clean up memory

**URL:** <https://root-forum.cern.ch/t/how-to-delete-rdataframe-and-clean-up-memory/33036>\
**Category:** ROOT\
**Tags:** rdataframe, python, tree, root\
**Created:** [March 5, 2019, 7:06pm UTC](https://root-forum.cern.ch/t/how-to-delete-rdataframe-and-clean-up-memory/33036 "2019-03-05T19:06:01Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [March 5, 2019, 8:04pm UTC](https://root-forum.cern.ch/t/how-to-delete-rdataframe-and-clean-up-memory/33036/2 "2019-03-05T20:04:50Z")

</div>

Hi Daniil,  
the `del` at the end of `loopDataFrame` is redundant, `df` is deleted at the end of the function call. @etejedor can correct me if I’m wrong, but I _think_ PyROOT guarantees that when the last python variable referencing a given python object goes out of scope, the underlying C++ object is deleted.

So even without the `del`, the C++ `RDataFrame` object should go out of scope and its memory should be freed.

In your case, the question is rather “what is allocating memory and never freeing it?”. It’s possible that the culprit is the ROOT interpreter: `RDataFrame` just-in-time compiles (jits) C++ code (for example, `"lep_0_p4.Pt()"` is jitted into a corresponding C++ function, and `Histo1D(model, "myP4")` is jitted in the template call `Histo1D<float>(model, "myP4")`.  
The memory allocated by the ROOT interpreter contains _code_, and is never released.

I’m not sure what else might contribute to that memory usage. One could check whether the corresponding C++ program (with and without jitted calls) uses less memory and how much. Or a tool like `valgrind --tool=massif` might help in identifying memory hoggers.

I’m available for further clarifications!  
Cheers,  
Enrico

---

_[View the full topic](https://root-forum.cern.ch/t/how-to-delete-rdataframe-and-clean-up-memory/33036)._
