# Remove memory from Julia's management

**URL:** <https://discourse.julialang.org/t/remove-memory-from-julias-management/133983>\
**Category:** General Usage\
**Tags:** memory-allocation, ffi, api, gc\
**Created:** [November 19, 2025, 8:50am UTC](https://discourse.julialang.org/t/remove-memory-from-julias-management/133983 "2025-11-19T08:50:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chrysoberyl](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@Chrysoberyl](https://discourse.julialang.org/u/Chrysoberyl)\
**Post date:** [November 19, 2025, 8:50am UTC](https://discourse.julialang.org/t/remove-memory-from-julias-management/133983/1 "2025-11-19T08:50:09Z")

</div>

When calling into Julia and using it as a library, is it possible to remove an allocated datum from Julia’s management?

My use case is this: I have a Julia function, passed across FFI as a callback, which creates data by calling functions in C, and it needs to return this data out of Julia. When this happens, the memory management obligation of this data is no longer Julia’s.

Alternatively, would it be possible to remove a pointer finalizer on the data? This way, any associated finalizers introduced by the FFI would not get called. I looked at the source code [julia/src/gc-common.c at cd2dc914625f1eec549eb32332f1e4c4f0406152 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/cd2dc914625f1eec549eb32332f1e4c4f0406152/src/gc-common.c#L474) and it would not be too difficult to write such a function.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [November 19, 2025, 10:26am UTC](https://discourse.julialang.org/t/remove-memory-from-julias-management/133983/2 "2025-11-19T10:26:06Z")

</div>

> [@Chrysoberyl](#):
>
> Alternatively, would it be possible to remove a pointer finalizer on the data?

The simplest way would be to change the pointer in the object to defuse the finalizer.

Suppose your situation is

```julia-auto
julia> mem = unsafe_wrap(Memory{Int8}, ccall(:malloc, Ptr{Int8}, (Csize_t,), 123), 123);
julia> finalizer(m -> ccall(:free, Cvoid, (Ptr{Nothing},), m.ptr), mem);

```

Then, you simply need to

```julia-auto
julia> mem.ptr = C_NULL
ERROR: setfield!: const field .ptr of type GenericMemory cannot be changed

```

You can’t do that with good syntax from julia, but you can definitely do that from C (alternatively, do some `unsafe_store!`).

Note that this makes you depend on the way the finalizer was written. This will fail horribly if someone “refactors” the code into

```julia-auto
julia> finalizer(m -> ccall(:free, Cvoid, (Ptr{Nothing},), mem.ptr), mem);

```

(that would capture the value of `mem.ptr`)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 19, 2025, 12:19pm UTC](https://discourse.julialang.org/t/remove-memory-from-julias-management/133983/3 "2025-11-19T12:19:01Z")

</div>

> [@Chrysoberyl](#):
>
> which creates data by calling functions in C, and it needs to return this data out of Julia. When this happens, the memory management obligation of this data is no longer Julia’s.

If you create the data by calling a C function that returns a pointer, I don’t see the problem — Julia doesn’t manage the memory.

That’s still true if you use `unsafe_wrap` to wrap a Julia array around the pointer — the default behavior is `own=false`, which means that Julia doesn’t manage the memory.
