# Check if a value is rooted by the Garbage Collector (GC)

**URL:** https://discourse.julialang.org/t/check-if-a-value-is-rooted-by-the-garbage-collector-gc/78887
**Category:** General Usage
**Tags:** question
**Created:** [April 1, 2022, 8:03pm UTC](https://discourse.julialang.org/t/check-if-a-value-is-rooted-by-the-garbage-collector-gc/78887 "2022-04-01T20:03:56Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Clemapfel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/clemapfel/32/32261_2.png) [@Clemapfel](https://discourse.julialang.org/u/Clemapfel)
#### Post date: [April 1, 2022, 8:03pm UTC](https://discourse.julialang.org/t/check-if-a-value-is-rooted-by-the-garbage-collector-gc/78887/1 "2022-04-01T20:03:56Z")

</div>

Given a C-pointer `jl_value_t*`, I would like to check whether the GC currently has a root to it. With root, I mean that the memory is marked such that it cannot be released when the gc starts its sweeping process.

I’m pretty sure this is only solvable though the julia C-API, which I am fine with. Looking at [julia\_gcext.h](https://github.com/JuliaLang/julia/blob/master/src/julia_gcext.h) I found no function relevant to this problem.  
My current best lead is [this field of the thread heap in julia\_threads.h](https://github.com/JuliaLang/julia/blob/master/src/julia_threads.h#L151), but I’m unsure of how to interact with it. None of this is documented, afaik.

If I could somehow get notified when the GC roots a value, that would solve it too. Any ideas?

* * *

EDIT: I am very concious of the fact I shouldn’t be rummaging around the C-APIs internals, but I through careful consideration I have come to the conclusion that it is necessary. Even if it is a bad idea, I would still like to know how to access the list of values

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [April 1, 2022, 8:09pm UTC](https://discourse.julialang.org/t/check-if-a-value-is-rooted-by-the-garbage-collector-gc/78887/2 "2022-04-01T20:09:52Z")

</div>

I think you are thinking about this backwards. You should always root an object before calling into C, so from C’s perspective, all objects it can see should be rooted.

---

<div class="post-metadata">

### Author: ![Clemapfel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/clemapfel/32/32261_2.png) [@Clemapfel](https://discourse.julialang.org/u/Clemapfel)
#### Post date: [April 1, 2022, 8:16pm UTC](https://discourse.julialang.org/t/check-if-a-value-is-rooted-by-the-garbage-collector-gc/78887/3 "2022-04-01T20:16:33Z")

</div>

I realize this, however, for my specific problem, I cannot be certain a given object is already rooted. That is up to a third party, and I have no control over it.

I am currently rooting every single object to make sure, which incurs a performance overhead for cases where it is already is rooted. I’m trying to avoid this, hence my question.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [April 1, 2022, 8:20pm UTC](https://discourse.julialang.org/t/check-if-a-value-is-rooted-by-the-garbage-collector-gc/78887/4 "2022-04-01T20:20:47Z")

</div>

In that case, you should probably disable gc while the ccall is running.

---

<div class="post-metadata">

### Author: ![Clemapfel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/clemapfel/32/32261_2.png) [@Clemapfel](https://discourse.julialang.org/u/Clemapfel)
#### Post date: [April 1, 2022, 8:42pm UTC](https://discourse.julialang.org/t/check-if-a-value-is-rooted-by-the-garbage-collector-gc/78887/5 "2022-04-01T20:42:28Z")

</div>

My API has C as the host language, I have no control over the amount of time any given value may be needed, hence disabling the GC is not an option. I want to avoid asking the user to manually disable and reenable the GC. I’m trying to both automate that process, and avoid doing the double rooting, which is the reason I posed the question.

I know that it is bad practice and probably a bad idea, but I still want to know how to access the list of rooted values anyway

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [April 2, 2022, 2:20pm UTC](https://discourse.julialang.org/t/check-if-a-value-is-rooted-by-the-garbage-collector-gc/78887/6 "2022-04-02T14:20:28Z")

</div>

> [@Clemapfel](#):
>
> I am currently rooting every single object to make sure, which incurs a performance overhead for cases where it is already is rooted

Way way less than running the mark process which is the only way you’ll know for sure that it’s rooted I suspect.
