# How to clean/delete properly RooDataSet in PyROOT?

**URL:** <https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749>\
**Category:** Roofit and RooStats\
**Created:** [March 12, 2026, 11:52am UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749 "2026-03-12T11:52:42Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![ibelyaev](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ibelyaev/32/16043_2.png) [@ibelyaev](https://root-forum.cern.ch/u/ibelyaev)\
**Post date:** [March 12, 2026, 11:52am UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/1 "2026-03-12T11:52:43Z")

</div>

What is the correct/recommended way to clean/delete RooDataSet in PyROOT ?

I have a use case where relatively large dataset is created and used inside the loop. and I see that memory is not free after the loop.

```auto

import ROOT, os, psutil, random 

# inner psutil function
def process_memory():
    process = psutil.Process(os.getpid())
    mem_info = process.memory_info()
    return mem_info.rss

evt = ROOT.RooRealVar ( 'Evt' , '#event' , 0 , 1000000 )
run = ROOT.RooRealVar ( 'Run' , '#run' , 0 , 1000000 )
mass = ROOT.RooRealVar ( 'Mass' , 'mass-variable' , 0 , 100 )

varset = ROOT.RooArgSet ( evt , run , mass )
dataset = ROOT.RooDataSet ( "dataset", 'Test Data set-0' , varset )  

before_create = process_memory ()
print ( 'MEMORY BEFORE CREATION OF DATASET %.1fMB' % ( before_create / 2**20 ) ) 

NR = 1000
NE = 1000

for r in range ( NR ) :
    run.setVal ( r )
    for e in range ( NE ) :        
        evt .setVal ( e )        
        mass.setVal ( random.uniform ( 0 , 10 ) )
        dataset.add ( varset )

before_loop = process_memory ()
print ( 'DELTA MEMORY (BEFORE LOOP) %.2fMB' % ( ( before_loop - before_create ) / 2**20 ) ) 

NL = 100 
for i in range ( NL ) :

    ## some actinon to create another large dataset, e,g, resuction
    ## in my concreete case I create the boostrapped-dataset 
    another = dataset.reduce ( "Mass<100" ) 

    ## (1) 
    ## del another ## No effect at all 

    ## (2) 
    ## another.Delete() ## it works, but looks a bit brutal 

    ## (3) -- no effect 
    ## store = another.store()
    ## if store :
    ## store.reset ()
    ## store.resetBuffers ()
    ## store.resetCache ()        
    ## another.resetBuffers ()
    ## another.reset ()
     
    
after_loop = process_memory ()
print ( 'DELTA MEMORY (AFTER LOOP) %.2fMB' % ( ( after_loop - before_loop ) / 2**20 ) )
    

 

```

- simple `delete another` inside the loop has no effece
- `another.Delete()` does work but cout it be a recomemnded way?
- manual reset of store and dataset does report that `another` has no events enynmore, but memory is not free.

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [March 13, 2026, 2:02pm UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/2 "2026-03-13T14:02:10Z")

</div>

Dear @ibelyaev ,

Thank you for reaching out to the forum! Indeed I do not expect any `del` call in Python to have any immediate or predictable effect, that’s just how the CPython garbage collector works. A somewhat more reliable way would be to restrict variables allocating a lot of memory to a free function scope, taking care that those variables are not referenced ever by anything outside that function scope.

For the particular RooFit classes, maybe @jonas will have an idea on how to help more concretely.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [March 13, 2026, 3:11pm UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/3 "2026-03-13T15:11:27Z")

</div>

Ah yes, that’s a classic. The `RooAbsData::reduce()` method returns the dataset by pointer, and it has to be manually deleted on the C++ side. PyROOT doesn’t know that it has to do that, because it’s not clear to know it automatically from the C++ interface. And we can’t easily change the C++ interface to smart pointers because of backwards incompatibility…

The solution is to tell PyROOT that it owns the returned object and needs to delete it:

```python
    another = dataset.reduce ( "Mass<100" )
    ROOT.SetOwnerhip(another, True)

```

Does this work? I’ll also update PyROOT to do this automatically when you create a dataset with `reduce()`.

---

<div class="post-metadata">

**Author:** ![ibelyaev](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ibelyaev/32/16043_2.png) [@ibelyaev](https://root-forum.cern.ch/u/ibelyaev)\
**Post date:** [March 13, 2026, 3:25pm UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/4 "2026-03-13T15:25:26Z")

</div>

Hi @jonas

Thank you for solution.

I have several appearence of such pattern (creation of RoDataSet in loop via variosu methods - not only `reduce` - e.g. for pseudioexperiments)

`ROOT.SetOwnership` trick works for _some_ ROOT(Python?) versions - In my taste I run over LCG\_102 -…-LCG\_108-..-dev3 slot, ROOT 6.26-6.39). I’ve found that explicit instatiation of `std::unique_ptr` works almost for all ROOT versions

```auto
data_ptr = std.unique_ptr(ROOT.RooabsData)

...

for .... :

   ds = data_ptr ( dataset.reduce (... ) ) 
   ...
   del ds 

 

```

such construction works for all ROOT versions except 6.32-6.34

Of source I’d like to have some “universal” solution, avoiding “if”-branches on ROOT versions

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [March 13, 2026, 3:39pm UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/5 "2026-03-13T15:39:28Z")

</div>

That’s quite surprising! I have no answer to that yet. Can you create an overview on which method works for which ROOT version?

For the `unique_ptr`: maybe it helps to create it with template instantiations using the bracket operator: `std.unique_ptr[ROOT.RooAbsData]`, because this is less ambiguous.

---

<div class="post-metadata">

**Author:** ![ibelyaev](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ibelyaev/32/16043_2.png) [@ibelyaev](https://root-forum.cern.ch/u/ibelyaev)\
**Post date:** [March 13, 2026, 4:27pm UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/6 "2026-03-13T16:27:46Z")

</div>

Hi Jonas,

I’ve repeated my tests with ROOT.SetOwnership and std::unique\_ptt

- Good news: if the dataset inside the loop is created using `reduce` both methods work os for all ROOT version between 6.26 and 6.39
- If the dataset inside the loop is prepare as “Jeckknife”, namely

```auto
 original_dataset = ...
 for i in range ( len ( original_dataset ) ) :
       ds1 = original_dataset.reduce ( ROOT.RooFit.EventRange ( ... ))  
       ds2 = original_dataset.reduce ( ROOT.RooFit.EventRange ( ... ) ) 
       
       ds = <merge ds1 and ds2>

       ...
       del ds1
       del ds2 
       del ds   

```

for such scenario, BOTH methods fail for ROOT verisions 6.32-6.34, while are OK for other ROOT versions. And the amount of “memory leak” corresponds to `ds` (`ds1` and `ds2` are ok)

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [March 13, 2026, 4:44pm UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/7 "2026-03-13T16:44:12Z")

</div>

Thanks for the details! How do you `<merge ds1 and ds2>` though?

---

<div class="post-metadata">

**Author:** ![ibelyaev](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ibelyaev/32/16043_2.png) [@ibelyaev](https://root-forum.cern.ch/u/ibelyaev)\
**Post date:** [March 13, 2026, 4:50pm UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/8 "2026-03-13T16:50:24Z")

</div>

```auto
 result = ds1.emptyClone( <unique name> ) 
 result.append ( ds1 )
 result.append ( ds2 )

```

---

<div class="post-metadata">

**Author:** ![ibelyaev](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ibelyaev/32/16043_2.png) [@ibelyaev](https://root-forum.cern.ch/u/ibelyaev)\
**Post date:** [March 13, 2026, 5:42pm UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/9 "2026-03-13T17:42:20Z")

</div>

For ROOT 6.32-6.34 I’ve got success only using a brutal `ds.Delete()`

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [March 14, 2026, 9:49am UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/10 "2026-03-14T09:49:16Z")

</div>

Dear @ibelyaev ,

Note that a still brutal but maybe more “standard-looking” approach could be to call the `obj. __destruct__ ()` method which is available to any Python proxy, see [Classes — cppyy 3.5.0 documentation](https://cppyy.readthedocs.io/en/latest/classes.html#destructors). I still believe Jonas’s approach is the correct one, but it won’t be automatically available to you retroactively in all older ROOT releases.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![ibelyaev](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ibelyaev/32/16043_2.png) [@ibelyaev](https://root-forum.cern.ch/u/ibelyaev)\
**Post date:** [March 14, 2026, 11:06am UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/11 "2026-03-14T11:06:11Z")

</div>

Thank you @vpadulan

` obj. __destruct__ ()` looks a bit less brutal than `obd.Delete()`

I can only agree that Jonas’s approach looks better. (Moreover , one can easily hide `SetOwnership`` into pythonized `reduce` )

In a meantime I’ll try to use some ifs based on ROOT version

---

<div class="post-metadata">

**Author:** ![ibelyaev](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ibelyaev/32/16043_2.png) [@ibelyaev](https://root-forum.cern.ch/u/ibelyaev)\
**Post date:** [March 14, 2026, 1:21pm UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/12 "2026-03-14T13:21:42Z")

</div>

Dear @vpadulan , @jonas

I’ve played with several combinations of ` __destruct__ `` , `Delete`, and `SetOwnership`

As noted earler, the actual action heavily depends of ROOT version.

Surprisingly (at least for me) ` __destruct__ ` action is not equal to `Delete` action.

The solution suggected by Jonas apepars to be the most useful however I need to add addtional ` __destruct__ ` call.  
My final solution (more complicated that toy example above, but still simplified versus the actual code) is [here](https://gist.github.com/VanyaBelyaev/fa490147fa3885f1ee00469cf70771e5)

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [March 14, 2026, 2:46pm UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/13 "2026-03-14T14:46:57Z")

</div>

Dear @ibelyaev ,

I believe you are still mixing two different things in your example. The Pythonization concept introduced by Jonas ensures there is no memory leak of the C++ allocated objects at the end of the Python application. But, attempts of freeing memory at specific points of your applications, e.g. `obj.Delete()` or `obj. __destruct__ ()` are going beyond the standard Python behaviour. In Python, there is no guarantee of when the garbage collector will run and thus there is no guarantee regarding when your memory will be freed. Thus, when you call `if delete : result . __destruct__ () ` in line 90 of your gist, you are arbitrarily using a feature of cppyy, and this is not something you should expect your Python application to handle automatically in any case, whether you are using ROOT or any other Python package.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![ibelyaev](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ibelyaev/32/16043_2.png) [@ibelyaev](https://root-forum.cern.ch/u/ibelyaev)\
**Post date:** [March 14, 2026, 2:50pm UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/14 "2026-03-14T14:50:52Z")

</div>

Dear @vpadulano

Thank you for explanation.

But finaly I need some way to ensure that for _large_ loops _large_ datasets created internally are somehow deleted. In my case datasets and loops are really large - without some actions I easyley get many gigabytes of memory.

what is the proper solution ?

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [March 14, 2026, 2:55pm UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/15 "2026-03-14T14:55:52Z")

</div>

Dear @ibelyaev ,

Apologies if I gave the impression that your code was somehow wrong. It isn’t wrong per se. I just wanted to point out that the need to deallocate memory in a Python application at a very specific line of code is just not part of the language. In your case, you have this need, and you use a public API of a package that allows to accomplish what you need, thus you are actively using a feature of the package. Nothing wrong with that.

Perhaps you could try to wrap the parts of the loop that allocate memory in a function scope, and see if the garbage collector does a better job without the need to explicitly access the feature of using ` __destruct__ `. This might work since if the variable in a function scope has no other references in the Python application then the garbage collector should delete it. But this wouldn’t be any more correct than what you are currently doing.

Cheers,  
Vincenzo

---

<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:** [March 28, 2026, 2:56pm UTC](https://root-forum.cern.ch/t/how-to-clean-delete-properly-roodataset-in-pyroot/64749/16 "2026-03-28T14:56:36Z")

</div>

Closed on March 28, 2026, 2:56pm UTC

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