# Julia v0.6 vs. v0.7 GC behaviour?

**URL:** <https://discourse.julialang.org/t/julia-v0-6-vs-v0-7-gc-behaviour/10002>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 27, 2018, 7:34am UTC](https://discourse.julialang.org/t/julia-v0-6-vs-v0-7-gc-behaviour/10002 "2018-03-27T07:34:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [March 27, 2018, 7:34am UTC](https://discourse.julialang.org/t/julia-v0-6-vs-v0-7-gc-behaviour/10002/1 "2018-03-27T07:34:13Z")

</div>

I’m a bit unclear about the new v0.7 GC behaviour - I read that GC is more aggressive than on v0.6, but I failed to locate the details.

In Julia v0.7, may a local variable be GC’ed within a function if it’s not used anymore? Can a function argument be GC’ed? So, for example:

```julia
function foo()
    A = rand(100)
    bar(A)
    # ...
    # A not used from here on
    # ....
end

function bar(A::Array)
    # do something with A
    # ...
    # A not used from here on
    # ....
end

```

Might A be GC’ed after `# A not used from here on` in `foo` and/or in `bar` on Julia v0.7?

When do I need to use the new `GC.@preserve` (assuming I need to pass a pointer to `A` to some code, not necessarily via `ccall`)?

Sorry if there’s already documentation on this - I’d be thankful for a pointer.

---

<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:** [March 27, 2018, 7:36am UTC](https://discourse.julialang.org/t/julia-v0-6-vs-v0-7-gc-behaviour/10002/2 "2018-03-27T07:36:18Z")

</div>

It can be GC:ed at any point. AFAIU only `cconvert` and `GC.@preserve` have special hooks into the GC to prevent objects being collected.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [March 27, 2018, 7:38am UTC](https://discourse.julialang.org/t/julia-v0-6-vs-v0-7-gc-behaviour/10002/3 "2018-03-27T07:38:18Z")

</div>

Thanks! Just for reference - on v0.6, `A` is safe from GC at least during `bar(A)`, right?

---

<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:** [March 27, 2018, 8:04am UTC](https://discourse.julialang.org/t/julia-v0-6-vs-v0-7-gc-behaviour/10002/4 "2018-03-27T08:04:38Z")

</div>

I am not sure of how clever the GC is in 0.6 but there is nothing holding `A` alive so it would not be a bug to GC it inside `bar`.

---

<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:** [March 27, 2018, 10:42am UTC](https://discourse.julialang.org/t/julia-v0-6-vs-v0-7-gc-behaviour/10002/5 "2018-03-27T10:42:19Z")

</div>

> [@kristoffer.carlsson](#):
>
> AFAIU only cconvert and GC.@preserve

It’s `ccall` and `GC.@preserve`. `cconvert` in other context is otherwise just a normal function.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [March 28, 2018, 5:52pm UTC](https://discourse.julialang.org/t/julia-v0-6-vs-v0-7-gc-behaviour/10002/6 "2018-03-28T17:52:55Z")

</div>

> [@oschulz](#):
>
> Can a function argument be GC’ed

No (if you mark the containing function `@noinline`) – function arguments are always protected from GC. This (along with the new `GC.@preserve`) can help when writing unsafe code which escapes from the provenance of a single `ccall` (which handles gc-protection automatically).

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [March 28, 2018, 8:05pm UTC](https://discourse.julialang.org/t/julia-v0-6-vs-v0-7-gc-behaviour/10002/7 "2018-03-28T20:05:44Z")

</div>

> [@jameson](#):
>
> No (if you mark the containing function @noinline) – function arguments are always protected from GC.

Thanks, that’s good to know. Will this also be the case on v0.7/v1.0?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 28, 2018, 8:24pm UTC](https://discourse.julialang.org/t/julia-v0-6-vs-v0-7-gc-behaviour/10002/8 "2018-03-28T20:24:30Z")

</div>

There are no long-term guarantees about when something can be GC’d. It will likely continue to be the case in 0.7/1.0 but it might change in any 1.x version. If you need something not to be collected, use `GC.@preserve` — that’s the only thing that’s guaranteed to work. Anything else is an implementation detail.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [March 29, 2018, 8:08am UTC](https://discourse.julialang.org/t/julia-v0-6-vs-v0-7-gc-behaviour/10002/9 "2018-03-29T08:08:53Z")

</div>

Thanks for the clarifications!
