ResetSession not clearing memory

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

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.

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

Do you reset session in your script ?

Please share a demo script to reproduce the issue.

I had previously opened an issue here (Paraview ResetSession causing crashes - #3 by ihelt) 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).

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:

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.

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

        Delete(self.enReader)
        ResetSession()
        import gc
        gc.collect()
1 Like

Thank you Mathieu! This seemed to do it:

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

1 Like

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

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 ?