# Garbage collector and memory management

**URL:** <https://discourse.julialang.org/t/garbage-collector-and-memory-management/17214>\
**Category:** New to Julia\
**Created:** [November 6, 2018, 12:03pm UTC](https://discourse.julialang.org/t/garbage-collector-and-memory-management/17214 "2018-11-06T12:03:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![circonflexe](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@circonflexe](https://discourse.julialang.org/u/circonflexe)\
**Post date:** [November 6, 2018, 12:03pm UTC](https://discourse.julialang.org/t/garbage-collector-and-memory-management/17214/1 "2018-11-06T12:03:20Z")

</div>

I have a data type that holds pointers to C data (for passing to a library):

```julia
mutable struct Data
   ptr::Array{Ptr{Int},1}
end

```

When a `Data` object is released, I want to perform some operations on the pointers to free the corresponding memory (i.e. pass them to some function in my C library). The first thought would be to attach a finalizer function in the constructor such as this:

```julia
Data(...) = begin
  self = ... # perform initialization and then
  finalizer(kill, self)
end

kill(x::Data) = begin
  # release C pointers here
end

```

_However_, in my case some of the C pointers might be referenced by multiple `Data` objects. This means that I cannot simply free them each time a `Data` object becomes inaccessible Julia-side.

I can imagine one and a half way to do this:

- the first way would be to manually implement reference-counting for the C pointers (increment refcount in the constructor, decrement it in the finalizer);
- the second one would be to somehow attach the pointers to Julia’s tracing GC.

The second way feels more elegant/efficient, but I’m not quite sure how to do this. Maybe I could wrap a trivial Julia type around the C pointer, make my `Data` struct contain pointers to these wrappers (keeping these alive as long as the `Data` is), and then do the pointer freeing at the wrapper level? How then can I be sure that, if a certain pointer is referenced more than once, all the wrappers will be the same Julia object (and thus the finalizer will only be invoked when the _last_ one of them dies)? Should I make the wrappers `Val{my_pointer}()` values?

Thanks,

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 6, 2018, 12:11pm UTC](https://discourse.julialang.org/t/garbage-collector-and-memory-management/17214/2 "2018-11-06T12:11:32Z")

</div>

> [@circonflexe](#):
>
> The second way feels more elegant/efficient

Not really.

This is C-interop so you should do it the same way you would do in C.

---

<div class="post-metadata">

**Author:** ![stemann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stemann/32/4030_2.png) [@stemann](https://discourse.julialang.org/u/stemann)\
**Post date:** [February 12, 2019, 3:19pm UTC](https://discourse.julialang.org/t/garbage-collector-and-memory-management/17214/3 "2019-02-12T15:19:33Z")

</div>

Any knowledge of a general implementation of the first alternative, i.e. reference counting? E.g. reference counted resources, arrays, C-allocated objects etc.

See also [Resource pooling - to avoid dynamic allocation](https://discourse.julialang.org/t/resource-pooling-to-avoid-dynamic-allocation/16921)
