# How to liberate python object allocated memory in mojo?

**URL:** <https://forum.modular.com/t/how-to-liberate-python-object-allocated-memory-in-mojo/515>\
**Category:** Mojo\
**Tags:** discussion, mojo-compiler, python-interop, 24\_6\
**Created:** [February 1, 2025, 9:56pm UTC](https://forum.modular.com/t/how-to-liberate-python-object-allocated-memory-in-mojo/515 "2025-02-01T21:56:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kraudy](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/kraudy/32/185_2.png) [@kraudy](https://forum.modular.com/u/kraudy)\
**Post date:** [February 1, 2025, 9:56pm UTC](https://forum.modular.com/t/how-to-liberate-python-object-allocated-memory-in-mojo/515/1 "2025-02-01T21:56:51Z")

</div>

When i run a mojo program that uses Python objects like

`var sklearn = Python.import_module("sklearn.datasets")`

using `build -sanitize address` i get a lot of indirect leak memory warnings by the mojo compiler. Is there a way to release the allocated memory for these python object manually?

So far, doing this does not seems to work: `sklearn = None`

 ![image](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/4f42b3deb809c9c3e02a449c4f8c7bc5847fb89a.png)

---

<div class="post-metadata">

**Author:** ![arthur](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/arthur/32/139_2.png) [@arthur](https://forum.modular.com/u/arthur)\
**Post date:** [February 4, 2025, 7:09pm UTC](https://forum.modular.com/t/how-to-liberate-python-object-allocated-memory-in-mojo/515/2 "2025-02-04T19:09:20Z")

</div>

You might also try explicitly discarding the value with `_ = sklearn^`

I don’t _think_ that’s going to work any differently than `sklearn = None`, but it’s worth ruling out. Other possibilities: accidental copying of the python object (for example, by passing it into an `owned` argument), or holding on to some value from `sklearn.datasets` that’s causing either Python or Mojo to retain the memory.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [August 3, 2025, 7:09pm UTC](https://forum.modular.com/t/how-to-liberate-python-object-allocated-memory-in-mojo/515/3 "2025-08-03T19:09:44Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
