# Is it possible to detect garbage collection on a custom struct?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-detect-garbage-collection-on-a-custom-struct/108338>\
**Category:** General Usage\
**Tags:** garbage-collection\
**Created:** [January 4, 2024, 12:34pm UTC](https://discourse.julialang.org/t/is-it-possible-to-detect-garbage-collection-on-a-custom-struct/108338 "2024-01-04T12:34:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThummeTo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thummeto/32/26105_2.png) [@ThummeTo](https://discourse.julialang.org/u/ThummeTo)\
**Post date:** [January 4, 2024, 12:34pm UTC](https://discourse.julialang.org/t/is-it-possible-to-detect-garbage-collection-on-a-custom-struct/108338/1 "2024-01-04T12:34:26Z")

</div>

Hi!

I am using an external library (LIB or DLL) to allocate and free memory that is not in the scope of the garbage collector, in the following example `externalAlloc` and `externalFree`. For better handling, I packed the memory pointer in a Julia struct.

```julia
struct myStruct
   ptr
   function myStruct()
      ptr = externalAlloc()
      return new(ptr)
   end
end

myInstance = myStruct()

```

The problem is, as soon as the user doesn’t interact with `myInstance` anymore and it get’s garbage collected, the allocated memory behind `ptr` is still in use (memory leak). So my favorite solution would be a “garbage collector notification dispatch” for `myStruct`, so that I can free the memory, like:

```julia
# this is called right before GC releases the memory for `myInstance`
function gcNotification(str::myStruct)
   externalFree(str.ptr)
   @info "Bye bye!"
end

```

Is there something like that out there? Or is there a better way to handle this process?

Thanks and best regards,  
ThummeTo

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [January 4, 2024, 12:38pm UTC](https://discourse.julialang.org/t/is-it-possible-to-detect-garbage-collection-on-a-custom-struct/108338/2 "2024-01-04T12:38:56Z")

</div>

You are looking for a finalizer: ?finalizer

---

<div class="post-metadata">

**Author:** ![ThummeTo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thummeto/32/26105_2.png) [@ThummeTo](https://discourse.julialang.org/u/ThummeTo)\
**Post date:** [January 4, 2024, 1:02pm UTC](https://discourse.julialang.org/t/is-it-possible-to-detect-garbage-collection-on-a-custom-struct/108338/3 "2024-01-04T13:02:48Z")

</div>

> [@vettert](#):
>
> ?finalizer

Ah yes, exactly what I was looking for! Thank you!

(only thing is `finalizer` only works for mutable structs)

To conclude my example, the solution would be:

```julia
mutable struct myStruct
   ptr
   function myStruct()
      ptr = externalAlloc()
      inst = new(ptr)
      return finalizer(finalize!, inst)
   end
end

function finalize!(str::myStruct)
   externalFree(str.ptr)
end

```
