# Slicer Memory Leak

**URL:** <https://discourse.slicer.org/t/slicer-memory-leak/22369>\
**Category:** Support\
**Created:** [March 8, 2022, 3:02pm UTC](https://discourse.slicer.org/t/slicer-memory-leak/22369 "2022-03-08T15:02:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabyan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fabyan/32/14594_2.png) [@Fabyan](https://discourse.slicer.org/u/Fabyan)\
**Post date:** [March 8, 2022, 3:02pm UTC](https://discourse.slicer.org/t/slicer-memory-leak/22369/1 "2022-03-08T15:02:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [March 8, 2022, 6:29pm UTC](https://discourse.slicer.org/t/slicer-memory-leak/22369/2 "2022-03-08T18:29:34Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![Fabyan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fabyan/32/14594_2.png) [@Fabyan](https://discourse.slicer.org/u/Fabyan)\
**Post date:** [March 8, 2022, 6:41pm UTC](https://discourse.slicer.org/t/slicer-memory-leak/22369/3 "2022-03-08T18:41:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![StephenLeePeng](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/stephenleepeng/32/11892_2.png) [@StephenLeePeng](https://discourse.slicer.org/u/StephenLeePeng)\
**Post date:** [September 28, 2023, 10:02am UTC](https://discourse.slicer.org/t/slicer-memory-leak/22369/4 "2023-09-28T10:02:46Z")

</div>

Have you resolved this issue?  
I am facing the same problem recently.  
And I wrote a topic,

> [@Strange Memory leak related with volumeNode and segmentationNode](https://discourse.slicer.org/t/strange-memory-leak-related-with-volumenode-and-segmentationnode/31936):
>
> Environment: Slicer 4.13, Linux Ubuntu 20.04. Background: Recently, I worked with a project, need to load some nii.gz file, vtp file. nii.gz file is loaded as vtkMRMLScalarVolumeNode, as the vtp file is loaded by Model, then transform to vtkSegmentationNode. nii.gz file was generated from a series dcm files by simpleITK, which shape is: 512x512x700, about 350MB. vtp files was segmented from volume, every vtp file is small part of volume. Firstly, Iily, I load vtp file as segmentationNode, …
