# Memory Usage Increase in Looped Script

**URL:** <https://discourse.slicer.org/t/memory-usage-increase-in-looped-script/3201>\
**Category:** Support\
**Created:** [June 15, 2018, 6:40pm UTC](https://discourse.slicer.org/t/memory-usage-increase-in-looped-script/3201 "2018-06-15T18:40:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Wei](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@Tom\_Wei](https://discourse.slicer.org/u/Tom_Wei)\
**Post date:** [June 15, 2018, 6:40pm UTC](https://discourse.slicer.org/t/memory-usage-increase-in-looped-script/3201/1 "2018-06-15T18:40:43Z")

</div>

Hello,

I’ve written a script that uses slicer.util.loadVolume() to open a scan in each iteration of a loop, and despite using slicer.mrmlScene.RemoveNode() each time, the memory usage still goes up in task manager, suggesting that the vtk object is still in memory somewhere and not freed. Once the memory fills up all the way, slicer will crash, so I guess I can’t count on the Python GC to take care of it either.

I suspected an extraneous reference count might be responsible for this, but observed the exact same effect even after calling UnRegister() to manually decrement it. Is there a proper way to free the memory associated with loading a volume?

Thanks

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 15, 2018, 6:57pm UTC](https://discourse.slicer.org/t/memory-usage-increase-in-looped-script/3201/2 "2018-06-15T18:57:14Z")

</div>

It may be simpler to clear the scene, but if that’s not an option then delete not just the volume node but also the volume’s display node and storage node.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)\
**Post date:** [June 15, 2018, 7:07pm UTC](https://discourse.slicer.org/t/memory-usage-increase-in-looped-script/3201/3 "2018-06-15T19:07:18Z")

</div>

I was just running a quick test while Andras replied – the memory does stay stable if you delete those two nodes.

```auto
>>> def loadLoop():
... slicer.util.loadVolume("/var/folders/7l/2qsp6sqx4_b5x9kf_yzzm4lc0000gn/T/Slicer/RemoteIO/MR-head.nrrd")
... n = getNode("MR-head*")
... slicer.mrmlScene.RemoveNode(n.GetStorageNode())
... slicer.mrmlScene.RemoveNode(n.GetDisplayNode())
... slicer.mrmlScene.RemoveNode(n)
... 
>>> for i in xrange(0,100):
... loadLoop()

```

If you want to find out what nodes are associated with a given data type, you can look at `slicer.mrmlScene.GetNodes()` before and after, with something like:

```auto
def getNodes():
  nodes = slicer.mrmlScene.GetNodes()
  return [nodes.GetItemAsObject(i).GetID() for i in xrange(0,nodes.GetNumberOfItems()]

nodes1 = getNodes()
#... do your load operation
nodes2 = getNodes()
filter(lambda x: x not in nodes1, nodes2)

```
