# Setting visibility to off in Volume Rendering does not unload the volume from texture memory

**URL:** <https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127>\
**Category:** Support\
**Created:** [December 18, 2020, 2:39am UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127 "2020-12-18T02:39:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/83011_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 18, 2020, 2:39am UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127/1 "2020-12-18T02:39:17Z")

</div>

As it is, once the volume is loaded onto the GPU texture memory, turning off its visibility does not unload it. Any subsequent volumes selected to render in the same session add to this memory usage (I am tracking through nvidia-smi). As far as I can tell, the only time they are unloaded is when the node is deleted, or scene is reset.

This behavior makes perfect sense in terms of performance and I don’t think it shouldn’t be changed. But I also think it might good to have a way to modify this behavior on-demand. It is a corner case, but an important one for people rendering large volumes on shared systems (linux). A typical use case would be running multiple docker instances using the same GPU.

---

<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:** [December 18, 2020, 3:46pm UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127/2 "2020-12-18T15:46:51Z")

</div>

Good point - as a workaround for now you can toggle between CPU and GPU rendering and that will flush the volumes from GPU memory.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/83011_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 18, 2020, 5:11pm UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127/3 "2020-12-18T17:11:12Z")

</div>

It does, but adds up quite a bit of time, because you need to enable CPU rendering, not just switch.

---

<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:** [December 18, 2020, 5:15pm UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127/4 "2020-12-18T17:15:38Z")

</div>

If you make the volume invisible first then switching is fast.

---

<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:** [December 18, 2020, 5:18pm UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127/5 "2020-12-18T17:18:49Z")

</div>

I’ve added an “Auto-release resources” checkbox to volume rendering module. If you check that that hiding a volume rendering also releases all graphics resources. Pull request is submitted: [https://github.com/Slicer/Slicer/pull/5352](https://github.com/Slicer/Slicer/pull/5352)

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/83011_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 18, 2020, 5:19pm UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127/6 "2020-12-18T17:19:59Z")

</div>

> [@pieper](#):
>
> If you make the volume invisible first then switching is fast.

Nope, doesn’t work with latest stable. It only flushes after it visibility set to on.

---

<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:** [December 18, 2020, 5:35pm UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127/7 "2020-12-18T17:35:11Z")

</div>

> [@muratmaga](#):
>
> It only flushes after it visibility set to on.

For me it works by toggling when the volume is not visible - this video shows nvidia-smi while doing the operations.

[https://dl.dropboxusercontent.com/s/868eatupnahb2a1/gpu-memory-volume-rendering-2020-12-18%20at%2012.29.57%20PM.mov?dl=0](https://dl.dropboxusercontent.com/s/868eatupnahb2a1/gpu-memory-volume-rendering-2020-12-18%20at%2012.29.57%20PM.mov?dl=0)

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/83011_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 18, 2020, 6:19pm UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127/8 "2020-12-18T18:19:50Z")

</div>

Himm, that’s not the behavior I see on Linux stable. Perhaps some driver etc version difference. In any event, @lassoan’s solution seems a good one. Particularly it can be set to be default, through .slicerrc.py or some sort of a starting script.

---

<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:** [December 18, 2020, 6:48pm UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127/9 "2020-12-18T18:48:13Z")

</div>

Default state of auto-release flag can be set in application settings the same way as other volume rendering settings (surface smoothing, etc).

---

<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:** [December 18, 2020, 7:22pm UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127/10 "2020-12-18T19:22:36Z")

</div>

Haha, while we were trying to still investigating Andras went ahead and fixed it! Thanks @lassoan 👍

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/83011_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [January 22, 2021, 4:46pm UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127/11 "2021-01-22T16:46:41Z")

</div>

> [@lassoan](#):
>
> I’ve added an “Auto-release resources” checkbox to volume rendering module. If you check that that hiding a volume rendering also releases all graphics resources. Pull request is submitted: [https://github.com/Slicer/Slicer/pull/5352](https://github.com/Slicer/Slicer/pull/5352)

I tried this on Linux r29631, and I didn’t see a difference in behavior when “auto release resources” is checked. I enabled volume rendering for MRHead. Checked with nvidia-smi that Slicer was using 104MB of texture memory, then I set its visibility off and rerun nvidia-smi which still reported 104MB being used by Slicer. If I delete the MRHead node, then this memory is released, but toggling visibility on/off doesn’t seem to unload the volume from GPU memory.

---

<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:** [January 22, 2021, 6:47pm UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127/12 "2021-01-22T18:47:57Z")

</div>

I see GPU memory usage decrease in Windows using renderdoc if I hide volume rendering while “Auto-release resources is enabled” (using Slicer 4.13.0-2021-01-16 (revision 29612 / d264109) win-amd64 - installed release).

Memory usage changes in Task Manager (Shared GPU memory), too, but those reports are less consistent. After a volume is hidden or removed not all memory is released, but when loading volume again, the memory is reused. So, it seems that the currently allocated memory is reported rather than the actually used amount. Maybe nvidia-smi reports something similar.

I would recommend to try volume rendering of very large volumes in multiple processes to confirm that memory is actually released when needed; or use a tool like renderdoc to get more reliable information about the actual state of the rendering pipeline.

* * *

If you want to try renderdoc:

- launch renderdoc using `Slicer --launch qrenderdoc`
- launch Slicer from it using File / Launch Application → SlicerApp-real
- capture frames before/after rendering and show/hide, etc.
- load each capture and check “Statistics” tab

For example:

After loading CTChest:

```auto
12 Textures - 27.05 MB (27.05 MB over 32x32), 6 RTs - 20.54 MB.
Avg. tex dimension: 963.333x758.333 (1155.6x909.6 over 32x32)
2 Buffers - 0.00 MB total 0.00 MB IBs 0.00 MB VBs.
47.59 MB - Grand total GPU buffer + texture load.

```

After showing volume rendering (140MB total GPU memory usage):

```auto
17 Textures - 166.07 MB (166.05 MB over 32x32), 7 RTs - 23.34 MB.
Avg. tex dimension: 936.4x506.5 (1048.33x843.333 over 32x32)
15 Buffers - 0.00 MB total 0.00 MB IBs 0.00 MB VBs.
189.41 MB - Grand total GPU buffer + texture load.

```

After hiding volume rendering (back to where it was before volume rendering):

```auto
12 Textures - 27.05 MB (27.05 MB over 32x32), 6 RTs - 20.54 MB.
Avg. tex dimension: 963.333x758.333 (1155.6x909.6 over 32x32)
9 Buffers - 0.00 MB total 0.00 MB IBs 0.00 MB VBs.
47.59 MB - Grand total GPU buffer + texture load.

```

After deleting the volume from the scene (no change in GPU memory usage):

```auto
12 Textures - 27.05 MB (27.05 MB over 32x32), 6 RTs - 20.54 MB.
Avg. tex dimension: 963.333x758.333 (1155.6x909.6 over 32x32)
15 Buffers - 0.00 MB total 0.00 MB IBs 0.00 MB VBs.
47.59 MB - Grand total GPU buffer + texture load.

```

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/83011_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [January 22, 2021, 11:17pm UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127/13 "2021-01-22T23:17:41Z")

</div>

> [@lassoan](#):
>
> If you want to try renderdoc:

I think VirtualGL I am using for remote connection is interfering with debugging. After launching SlicerApp-real, there is nothing to capture, and qrenderdoc shows lots of these errors:

```
Core 21855 15:14:50 gl_driver.cpp(3141) Log Got a Debug message from GL_DEBUG_SOURCE_API, type GL_DEBUG_TYPE_ERROR, ID 1282, severity GL_DEBUG_SEVERITY_HIGH:
Core 21855 15:14:50 gl_driver.cpp(3141) Log 'GL_INVALID_OPERATION error generated. Object is owned by another context and may not be bound here.'

```

I will try with a regular installation later.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/83011_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [January 22, 2021, 11:36pm UTC](https://discourse.slicer.org/t/setting-visibility-to-off-in-volume-rendering-does-not-unload-the-volume-from-texture-memory/15127/14 "2021-01-22T23:36:12Z")

</div>

> [@lassoan](#):
>
> I would recommend to try volume rendering of very large volumes in multiple processes to confirm that memory is actually released when needed;

Confirmed. WHen working with really large volumes (\>10GB+), I can see memory being released with Nvidia-smi when I uncheck the visibility. As you guessed MRhead was too small to see the differential.

Awesome, thank you!
