# Garbage collection

**URL:** <https://discourse.julialang.org/t/garbage-collection/41429>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 15, 2020, 10:09am UTC](https://discourse.julialang.org/t/garbage-collection/41429 "2020-06-15T10:09:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joao\_Barata](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joao_barata/32/10082_2.png) [@Joao\_Barata](https://discourse.julialang.org/u/Joao_Barata)\
**Post date:** [June 15, 2020, 10:09am UTC](https://discourse.julialang.org/t/garbage-collection/41429/1 "2020-06-15T10:09:18Z")

</div>

Hello,

I’m new to Julia. I ran my code and timed it using @time. The results show that gc time is 67.2%. From what I’ve seen in other threads, this is a high figure and is a drag on performance. However, it is unclear to me (even after reading the documentation) what garbage collection is, what the figure 67.2% means, and therefore what I might do to reduce it.

Any help is appreciated. thanks

---

<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:** [June 15, 2020, 10:41am UTC](https://discourse.julialang.org/t/garbage-collection/41429/2 "2020-06-15T10:41:20Z")

</div>

It would be much easier to help if you provided an MWE.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![Joao\_Barata](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joao_barata/32/10082_2.png) [@Joao\_Barata](https://discourse.julialang.org/u/Joao_Barata)\
**Post date:** [June 15, 2020, 11:04am UTC](https://discourse.julialang.org/t/garbage-collection/41429/3 "2020-06-15T11:04:42Z")

</div>

How can I provide a MWE if I don’t know what garbage collection is and what in my code is generating it?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 15, 2020, 11:33am UTC](https://discourse.julialang.org/t/garbage-collection/41429/4 "2020-06-15T11:33:30Z")

</div>

Not an actual expert on this, but garbage collection is basically freeing memory that you used in your calculation and is not required anymore. It can be a sign that you are overly reliant on creating lots of temporary arrays unnecessarily, which have to be allocated and freed, slowing down your code.

---

<div class="post-metadata">

**Author:** ![Joao\_Barata](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joao_barata/32/10082_2.png) [@Joao\_Barata](https://discourse.julialang.org/u/Joao_Barata)\
**Post date:** [June 15, 2020, 11:41am UTC](https://discourse.julialang.org/t/garbage-collection/41429/5 "2020-06-15T11:41:44Z")

</div>

Thanks for the explanation. So does that mean that 62.7% in gc time is that gc takes up 62.7% of the total time it takes to compute the code?

---

<div class="post-metadata">

**Author:** ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)\
**Post date:** [June 15, 2020, 11:44am UTC](https://discourse.julialang.org/t/garbage-collection/41429/6 "2020-06-15T11:44:31Z")

</div>

Garbage collection is how memory is recovered from your program. It is a form of automatic memory management: languages like python, r and Java also have automatic memory management. Without it you need to make explicit calls in your code to tell the operating system you are done using memory. Languages like c and c++ do not have automatic memory management. You can read more [here](https://en.m.wikipedia.org/wiki/Garbage_collection_(computer_science)).

If your program is spending a lot of time on garbage collection that often means you are allocating a lot of memory; e.g. creating lots of large arrays. Sometimes you can improve it by using mutable commands (e.g. sort! instead of sort).

Note that @time may not be accurate and you should probably use @btime from [BenchmarkTools](https://github.com/JuliaCI/BenchmarkTools.jl)

It’s hard to give you any more specific advice without seeing the specific code: that’s the advantage of a MWE.

---

<div class="post-metadata">

**Author:** ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)\
**Post date:** [June 15, 2020, 11:45am UTC](https://discourse.julialang.org/t/garbage-collection/41429/7 "2020-06-15T11:45:10Z")

</div>

Yes!

---

<div class="post-metadata">

**Author:** ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)\
**Post date:** [June 15, 2020, 11:48am UTC](https://discourse.julialang.org/t/garbage-collection/41429/8 "2020-06-15T11:48:06Z")

</div>

To be more specific 62% of the time the program was looking for blocks of memory that your program no longer needed and making them available for new data you were allocating. Garbage collection can take a long time if it has to rearrange blocks of memory or search through a lot of memory blocks, for example.

---

<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:** [June 15, 2020, 12:00pm UTC](https://discourse.julialang.org/t/garbage-collection/41429/9 "2020-06-15T12:00:37Z")

</div>

Maybe you can simplify the program to the point that it is small enough, yet it still displays a large amount of GC. This much garbage collection is somewhat unusual for idiomatic Julia code, but it is difficult to provide help without code to run.

If you are looking for an explanation of what GC is, the Wikipedia article maybe a good starting point:

> **[Garbage collection (computer science)](https://en.wikipedia.org/wiki/Garbage_collection_(computer_science))**
>
> In computer science, garbage collection (GC) is a form of automatic memory management. The garbage collector attempts to reclaim memory which was allocated by the program, but is no longer referenced; such memory is called garbage. Garbage collection was invented by American computer scientist John McCarthy around 1959 to simplify manual memory management in Lisp.
> Garbage collection relieves the programmer from doing manual memory management, where the programmer specifies what objects to de-...

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [June 15, 2020, 12:27pm UTC](https://discourse.julialang.org/t/garbage-collection/41429/10 "2020-06-15T12:27:43Z")

</div>

> [@haberdashPI](#):
>
> Note that @time may not be accurate and you should probably use @btime from [BenchmarkTools](https://github.com/JuliaCI/BenchmarkTools.jl)

I would suggest `@benchmark`; `@btime` wontl be accurate for %gc time since it picks the minimum.

Also, Julia’s GC is non-compacting, meaning it wont rearrange memory blocks.

Lots of allocations is also often a symptom of lots of dynamic dispatches.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 15, 2020, 12:56pm UTC](https://discourse.julialang.org/t/garbage-collection/41429/11 "2020-06-15T12:56:46Z")

</div>

My experience follows:

I had a method that when timed with TimeOutputs.jl gave a summed allocation of some GiB. This did not make sense to me as the method should deal with very small arrays that were just frequently rearranged. Then I discovered that `permute!` and `sort!` (using the keyword `by`) both allocated, and my code was an heuristic (that I had to reproduce, i.e., couldn’t just change the algorithm) which run at least one million iterations each one calling a small constant number of permutes and one sort. I had to reimplement such methods by hand for them to take a buffer vector, and after this my summed allocation ended up as some KiB, and the total time as 15~20% of the original time. So, it can depend on you having a very tight and long loop in which you call `Base` methods that allocate.
