# Give hints to Julia's GC about size of memory allocated through C calls

**URL:** <https://discourse.julialang.org/t/give-hints-to-julias-gc-about-size-of-memory-allocated-through-c-calls/2367>\
**Category:** General Usage\
**Tags:** ccall\
**Created:** [February 28, 2017, 3:21pm UTC](https://discourse.julialang.org/t/give-hints-to-julias-gc-about-size-of-memory-allocated-through-c-calls/2367 "2017-02-28T15:21:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [February 28, 2017, 3:21pm UTC](https://discourse.julialang.org/t/give-hints-to-julias-gc-about-size-of-memory-allocated-through-c-calls/2367/1 "2017-02-28T15:21:16Z")

</div>

In [SCIP.jl](https://github.com/SCIP-Interfaces/SCIP.jl), we build a wrapper for a C library. There’s a Julia type `SCIPModel` that basically holds a Pointer to a C struct. In the constructor, we call a corresponding initializer of the C library that allocates significant memory. The destructor is then called in the finalizer of our Julia type.

Generally, the garbage collection works find: At some point, when our Julia type is out of scope, it will be finalized and the memory of the C library freed as well. But I’ve noticed that this garbage collection is sometimes delayed a lot, to the point that my machine freezes because it runs out of memory (see [PR #34](https://github.com/SCIP-Interfaces/SCIP.jl/pull/34#issuecomment-282755114).)

My guess is that Julia knows when to call `gc()` before it runs out of memory, but it only knows about its _own_ memory, not the implicitely allocated one. So my question is: Can I make Julia’s GC aware of the (rought amount of) memory that _belongs_ to some of its objects?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 28, 2017, 3:42pm UTC](https://discourse.julialang.org/t/give-hints-to-julias-gc-about-size-of-memory-allocated-through-c-calls/2367/2 "2017-02-28T15:42:14Z")

</div>

For CHOLMOD in Julia base, it is using a configuration option in CHOLMOD so that the allocator used is the same one as julia does: [https://github.com/JuliaLang/julia/blob/bdef3b57d34ec90781d30f62adf73d048e4a3090/base/sparse/cholmod.jl#L157](https://github.com/JuliaLang/julia/blob/bdef3b57d34ec90781d30f62adf73d048e4a3090/base/sparse/cholmod.jl#L157).

Perhaps you could do something like that if the C library allows for it.

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [February 28, 2017, 3:48pm UTC](https://discourse.julialang.org/t/give-hints-to-julias-gc-about-size-of-memory-allocated-through-c-calls/2367/3 "2017-02-28T15:48:13Z")

</div>

Thanks @kristoffer.carlsson, that is a nice solution. Our C library implements its own (block) memory management and AFAIK the user can not tell it to use another `malloc` function.
