# ResetSession not clearing memory

**URL:** https://discourse.paraview.org/t/resetsession-not-clearing-memory/15975
**Category:** ParaView Support
**Tags:** python
**Created:** [January 13, 2025, 5:48pm UTC](https://discourse.paraview.org/t/resetsession-not-clearing-memory/15975 "2025-01-13T17:48:31Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![ihelt](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/i/5e9695/32.png) [@ihelt](https://discourse.paraview.org/u/ihelt)
#### Post date: [January 13, 2025, 5:48pm UTC](https://discourse.paraview.org/t/resetsession-not-clearing-memory/15975/1 "2025-01-13T17:48:31Z")

</div>

Hello,

I’ve been trying to use the ResetSession() function to clear memory in my paraview script, but that doesn’t seem to be working. Am I misunderstanding how memory is managed in paraview? Should I be using a different function? Delete() or del didn’t help at all either.

Thank you

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [January 14, 2025, 9:30am UTC](https://discourse.paraview.org/t/resetsession-not-clearing-memory/15975/2 "2025-01-14T09:30:16Z")

</div>

How do you check memory usage ? The OS is pretty lazy when it comes to reclaiming freed memory. If it doesnt need it, it will still show up as being used although it is available.

---

<div class="post-metadata">

### Author: ![ihelt](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/i/5e9695/32.png) [@ihelt](https://discourse.paraview.org/u/ihelt)
#### Post date: [January 15, 2025, 9:15pm UTC](https://discourse.paraview.org/t/resetsession-not-clearing-memory/15975/3 "2025-01-15T21:15:37Z")

</div>

I’ve just been using the top command on linux, but the issue comes in when my script eventually crashes due to a lack of memory and I get:  
error: exception occurred: Subprocess killed

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [January 16, 2025, 8:54am UTC](https://discourse.paraview.org/t/resetsession-not-clearing-memory/15975/4 "2025-01-16T08:54:23Z")

</div>

> [@ihelt](#):
>
> the issue comes in when my script eventually crashes due to a lack of memory

Do you reset session in your script ?

Please share a demo script to reproduce the issue.

---

<div class="post-metadata">

### Author: ![ihelt](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/i/5e9695/32.png) [@ihelt](https://discourse.paraview.org/u/ihelt)
#### Post date: [January 16, 2025, 2:19pm UTC](https://discourse.paraview.org/t/resetsession-not-clearing-memory/15975/5 "2025-01-16T14:19:09Z")

</div>

I had previously opened an issue here ([Paraview ResetSession causing crashes - #3 by ihelt](https://discourse.paraview.org/t/paraview-resetsession-causing-crashes/15751/3)) about ResetSession() causing a crash which had a similar minimally working example (in case this looks familiar), but the code below creates a new layout, which was a remedy suggested in issue 22831 ([https://gitlab.kitware.com/paraview/paraview/-/issues/22831](https://gitlab.kitware.com/paraview/paraview/-/issues/22831)).

```auto
from paraview.simple import *
import os

class pv:
    def __init__ (self,casename):
        print('Initializing...')
        self.enReader = EnSightReader(registrationName='case1', CaseFileName=casename)

        # Create layout: Remedy to ResetSession crashing (Paraview issue 22831)
        layout = CreateLayout()
        self.renderView = GetActiveViewOrCreate('RenderView')
        AssignViewToLayout(view=self.renderView, layout=layout)

    def __del__ (self):
        print('deleting...')
        Delete(self.enReader)
        ResetSession()

    def makeBlock(self):
        print('Making block...')
        self.extractBlock = ExtractBlock(registrationName='block', Input=self.enReader)
        self.extractBlockDisplay = Show(self.extractBlock, self.renderView, 'UnstructuredGridRepresentation')
        self.extractBlock.Selectors = ['booster']
        
    def makePicture(self,picName):
        pic_path = './'+picName+'.png'
        print('Saving '+picName+'...')
        
        layout = GetLayout()
        layout.SetSize(1400,200)
        if os.path.exists(pic_path):
            os.remove(pic_path)
        SaveScreenshot(pic_path, self.renderView, ImageResolution=[1400, 200], OverrideColorPalette='BlackBackground')
        

def main():

    # NOTE: "error: exception occurred: Subprocess killed" is a result of all memory being used by CPU
    casename1 = /path/to/file.case'

    i = 1
    while i < 10:
        print('-----------------------------------')
        pvi = pv(casename1)
        pvi.makeBlock()
        pvi.makePicture('pic{0:d}'.format(i))
        pvi. __del__ ()
        del pvi
        print('pv{0:d} done'.format(i))
        i = i+1

    print('done')

if __name__ == ' __main__':
    main()

```

Executing this code gives me the following output:

```auto
user:> pvpython --mesa replicateCrash.py
-----------------------------------
Initializing...
VisRTX 0.1.6, using devices:
 0: Quadro P2000 (Total: 5.3 GB, Available: 5.1 GB)
Making block...
Saving pic1...
deleting...
deleting...
pv1 done
-----------------------------------
Initializing...
Making block...
Saving pic2...
deleting...
deleting...
pv2 done
-----------------------------------
Initializing...
Making block...
Saving pic3...
deleting...
deleting...
pv3 done
-----------------------------------
Initializing...
Making block...
error: exception occurred: Subprocess killed

```

where the crash coincides with my memory usage being completely full.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [January 16, 2025, 2:23pm UTC](https://discourse.paraview.org/t/resetsession-not-clearing-memory/15975/6 "2025-01-16T14:23:43Z")

</div>

Make sure you delete the python handles and call the garbage collector, eg:

```auto
        Delete(self.enReader)
        ResetSession()
        import gc
        gc.collect()

```

---

<div class="post-metadata">

### Author: ![ihelt](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/i/5e9695/32.png) [@ihelt](https://discourse.paraview.org/u/ihelt)
#### Post date: [January 16, 2025, 3:08pm UTC](https://discourse.paraview.org/t/resetsession-not-clearing-memory/15975/7 "2025-01-16T15:08:19Z")

</div>

Thank you Mathieu! This seemed to do it:

```auto
import gc

def __del__ (self):
        print('deleting...')
        Delete(self.enReader)
        Delete(self.renderView)
        Delete(self.extractBlock)
        Delete(self.extractBlockDisplay)
        ResetSession()
        gc.collect()

```

There still appears to be a bit of residual memory usage even after the destructor is called, but I’m not worried about it for the moment

---

<div class="post-metadata">

### Author: ![wascott](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/w/8e8cbc/32.png) [@wascott](https://discourse.paraview.org/u/wascott)
#### Post date: [January 16, 2025, 5:57pm UTC](https://discourse.paraview.org/t/resetsession-not-clearing-memory/15975/8 "2025-01-16T17:57:21Z")

</div>

Shouldn’t ResetSession delete all of these objects? Isn’t this a defect in ResetSession?

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [January 17, 2025, 9:09am UTC](https://discourse.paraview.org/t/resetsession-not-clearing-memory/15975/9 "2025-01-17T09:09:34Z")

</div>

ResetSession is reseting the ParaView session, but It definitely should not cleanup python objects.

That being said, maybe it makes sense to add a ResetSessionAndCleanNameSpace to simple.py ?

---

<div class="post-metadata">

### Author: ![wascott](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/w/8e8cbc/32.png) [@wascott](https://discourse.paraview.org/u/wascott)
#### Post date: [January 23, 2025, 1:30am UTC](https://discourse.paraview.org/t/resetsession-not-clearing-memory/15975/10 "2025-01-23T01:30:54Z")

</div>

[https://gitlab.kitware.com/paraview/paraview/-/issues/22858](https://gitlab.kitware.com/paraview/paraview/-/issues/22858)

---

<div class="post-metadata">

### Author: ![otaolafr](https://discourse.paraview.org/user_avatar/discourse.paraview.org/otaolafr/32/3595_2.png) [@otaolafr](https://discourse.paraview.org/u/otaolafr)
#### Post date: [November 20, 2025, 6:46am UTC](https://discourse.paraview.org/t/resetsession-not-clearing-memory/15975/11 "2025-11-20T06:46:10Z")

</div>

and shoulndt be a middle ground? without resetSession, to only clean the deleted objects?  
I suspect that this is what it is supposed to do the Delete(filter) but it is not doing it for me at least, and if I call the ResetSession it will remove the other filters.  
TL;DR, if i have a bunch of filters and one is taking a lot of memory, and then i delete one of them and even if i also gc.collect() the ram consomption is still there, and well… if i run ResetSession it will clean up the memory but also (obviously) remove the other filters i would like to keep.
