# Potential memory leak due to unsafe use of UnityWebRequest

**URL:** <https://community.cesium.com/t/potential-memory-leak-due-to-unsafe-use-of-unitywebrequest/38030>\
**Category:** Cesium for Unity\
**Created:** [January 16, 2025, 5:23pm UTC](https://community.cesium.com/t/potential-memory-leak-due-to-unsafe-use-of-unitywebrequest/38030 "2025-01-16T17:23:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanaF](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/danaf/32/14000_2.png) [@DanaF](https://community.cesium.com/u/DanaF)\
**Post date:** [January 16, 2025, 5:23pm UTC](https://community.cesium.com/t/potential-memory-leak-due-to-unsafe-use-of-unitywebrequest/38030/1 "2025-01-16T17:23:45Z")

</div>

When running profiler analysis on a Unity project that loads large 3D tile models, we observe a growing memory usage caused by UnityWebRequest instances. We suspect that these instances are not being disposed of properly, leading to a memory leak.

Unity’s documentation recommends calling `Dispose()` after receiving the request response, or encapsulating the UnityWebRequest within a `using` statement to ensure proper disposal.

---

<div class="post-metadata">

**Author:** ![janine](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/janine/32/5135_2.png) [@janine](https://community.cesium.com/u/janine)\
**Post date:** [January 21, 2025, 5:25pm UTC](https://community.cesium.com/t/potential-memory-leak-due-to-unsafe-use-of-unitywebrequest/38030/2 "2025-01-21T17:25:12Z")

</div>

Hi @DanaF, welcome to the community!

Thank you for letting us know about this. Just for context, do you see this happening with your own data? Or could you also reproduce this with only assets that we provide through Cesium ion (Cesium World Terrain, Google Photorealistic 3D Tiles, etc.)? The latter would be easier for us to replicate.

If this involves your own data, then it’d be great if you could share a performance profile for this, so we can take a look ourselves. 😄

---

<div class="post-metadata">

**Author:** ![Kevin\_Ring](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/kevin_ring/32/2003_2.png) [@Kevin\_Ring](https://community.cesium.com/u/Kevin_Ring)\
**Post date:** [January 28, 2025, 11:09pm UTC](https://community.cesium.com/t/potential-memory-leak-due-to-unsafe-use-of-unitywebrequest/38030/3 "2025-01-28T23:09:36Z")

</div>

Thanks for bringing this our attention, @DanaF. I wrote an issue to address this here:

> <https://github.com/CesiumGS/cesium-unity/issues/550>
>
> Mentioned on the forum here:
> https://community.cesium.com/t/potential-memory-lea…k-due-to-unsafe-use-of-unitywebrequest/38030
> 
> \`UnityWebRequest\` has a \`Dispose\` method, which we should call when the request completes (or fails). We're currently explicitly disposing the "download handler" but not the request itself. This could lead to memory growth if the garbage collector doesn't clean these up right away. And it also creates unnecessary for the finalizer, which has performance implications.
> 
> \`UnityWebRequest\` also has \`disposeDownloadHandlerOnDispose\` and \`disposeUploadHandlerOnDispose\` properties, and they're set to true by default, so we can probably switch to disposing the request \_instead of\_ the handler.
