Slicer Memory Leak

Hello. I am working on a slicer module that loads .mha data, applies transformations from a .csv to each frame and displays the separate orientations. Currently I load the data and preprocess it by splitting the different orientations into separate .mha files for each frame and then saving them to an output file using SimpleITK. I then load them in and add them to the Slice Widget. I have a play button and a slider to scroll through the “time” if you will. My problem is while developing, I need to reload my module and in doing so I will need to reload my data. This seems to be causing memory leaks as the memory 3d Slicer is taking keeps increasing by around 1gb each time. I tried deleting the nodes and they are not there in the Data tab but still taking up memory. I was wondering if there was a way to just clear the loaded volumes, or better yet, a smarter way to display separate orientations while applying a transformation to them?

You need to be sure to release all references to data to be sure it is freed. That means clearing it from the scene but also making sure there are no python variables still referencing the data. Slicer itself should be good about freeing data when it’s deleted but if you can develop a sample script that indicates a memory leak in the core please let us know.

For developing script modules typically try to avoid freeing and reloading data if it’s big. I do something like this in my experimental scripts:

    try:
        node = slicer.util.getNode(name)
    except slicer.util.MRMLNodeNotFoundException:
        node = loader(path)
        node.SetName(name)
    return node
1 Like

Hi, I tried slicer.mrmlScene.RemoveNode() to clear the nodes. Slicer seems to remove them as they are gone in the Data Module, but the memory does not get freed. I am not sure if slicer is removing the allocated memory for loaded volumes? . As for avoiding freeing and reloading data I will try the given structure of code thanks