# Is the garbage collector non-compacting?

**URL:** https://discourse.julialang.org/t/is-the-garbage-collector-non-compacting/16335
**Category:** General Usage
**Created:** [October 15, 2018, 8:26am UTC](https://discourse.julialang.org/t/is-the-garbage-collector-non-compacting/16335 "2018-10-15T08:26:56Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Steven\_Sagaert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_sagaert/32/29578_2.png) [@Steven\_Sagaert](https://discourse.julialang.org/u/Steven_Sagaert)
#### Post date: [October 15, 2018, 8:26am UTC](https://discourse.julialang.org/t/is-the-garbage-collector-non-compacting/16335/1 "2018-10-15T08:26:56Z")

</div>

If I serialize a model to a file and then restart julia and read in that model again, the memory usage is much lower so I get the impression that the CG is non-compacting, i.e. memory becomes fragmented and a lot of memory is wasted. Is this correct?

If so are there any plans for a better GC?

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [October 15, 2018, 1:43pm UTC](https://discourse.julialang.org/t/is-the-garbage-collector-non-compacting/16335/2 "2018-10-15T13:43:35Z")

</div>

Afaiu, the GC will never ever become compacting, for reasons of C interoparability.

Consider pulling a `deepcopy` of your data. This is, by the way, not just for fragmentation but will also allocate such that any operation that traverses your structure in `deepcopy`-order is more cache-friendly.

---

<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: [October 15, 2018, 7:22pm UTC](https://discourse.julialang.org/t/is-the-garbage-collector-non-compacting/16335/3 "2018-10-15T19:22:12Z")

</div>

> [@foobar\_lv2](#):
>
> will never ever become compacting,

This is not entirely ture. C escapting is something that could be handled with moving. If the need really come up. Implementing that should only come after implementing all other tricks available though and the julia GC is very far from that.

---

<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: [October 15, 2018, 7:22pm UTC](https://discourse.julialang.org/t/is-the-garbage-collector-non-compacting/16335/4 "2018-10-15T19:22:46Z")

</div>

> [@Steven\_Sagaert](#):
>
> memory becomes fragmented and a lot of memory is wasted

Not as much. Size segregatted pool helps a lot here.

---

<div class="post-metadata">

### Author: ![Steven\_Sagaert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_sagaert/32/29578_2.png) [@Steven\_Sagaert](https://discourse.julialang.org/u/Steven_Sagaert)
#### Post date: [October 16, 2018, 7:42am UTC](https://discourse.julialang.org/t/is-the-garbage-collector-non-compacting/16335/5 "2018-10-16T07:42:23Z")

</div>

I’d love to see the GC design documented. Right now I couldn’t find anything. Java has several GCs and the working of each one is documented in detail.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 16, 2018, 7:48am UTC](https://discourse.julialang.org/t/is-the-garbage-collector-non-compacting/16335/6 "2018-10-16T07:48:19Z")

</div>

> [@Steven\_Sagaert](#):
>
> I’d love to see the GC design documented. Right now I couldn’t find anything.

[`gc.c`](https://github.com/JuliaLang/julia/blob/master/src/gc.c) is rather well-documented (in the code).
