# Enable Parallel Processing of Garbage Collections

**URL:** <https://discourse.julialang.org/t/enable-parallel-processing-of-garbage-collections/108308>\
**Category:** New to Julia\
**Created:** [January 3, 2024, 3:35pm UTC](https://discourse.julialang.org/t/enable-parallel-processing-of-garbage-collections/108308 "2024-01-03T15:35:34Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdas123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdas123/32/32142_2.png) [@bdas123](https://discourse.julialang.org/u/bdas123)\
**Post date:** [January 3, 2024, 3:35pm UTC](https://discourse.julialang.org/t/enable-parallel-processing-of-garbage-collections/108308/1 "2024-01-03T15:35:34Z")

</div>

Here is my code:

```julia
using IJulia
installkernel("Julia (4 threads)", env=Dict("JULIA_NUM_THREADS"=>"4"))
Threads.nthreads()

```

This code creates a new kernel with multiple threads **and works**

How can I modify this kernel so that there is parallel processing of the garbage collection?

I need to add the `--gcthreads=M` somewhere.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [January 3, 2024, 3:46pm UTC](https://discourse.julialang.org/t/enable-parallel-processing-of-garbage-collections/108308/2 "2024-01-03T15:46:13Z")

</div>

[https://julialang.github.io/IJulia.jl/stable/library/public/#IJulia.installkernel](https://julialang.github.io/IJulia.jl/stable/library/public/#IJulia.installkernel)

has an example of passing command-line flags, “-O3” specifically. Perhaps you can replace that with `"--gcthreads=$(M)"`.

---

<div class="post-metadata">

**Author:** ![bdas123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdas123/32/32142_2.png) [@bdas123](https://discourse.julialang.org/u/bdas123)\
**Post date:** [January 3, 2024, 3:52pm UTC](https://discourse.julialang.org/t/enable-parallel-processing-of-garbage-collections/108308/3 "2024-01-03T15:52:01Z")

</div>

Awesome.

So `Threads.nthreads()` lets me know the number of threads present.

Is there a command that lets me know the number of `gcthreads`?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 3, 2024, 3:56pm UTC](https://discourse.julialang.org/t/enable-parallel-processing-of-garbage-collections/108308/4 "2024-01-03T15:56:46Z")

</div>

`Threads.ngcthreads()`

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [January 3, 2024, 3:56pm UTC](https://discourse.julialang.org/t/enable-parallel-processing-of-garbage-collections/108308/5 "2024-01-03T15:56:48Z")

</div>

You can also set the number of garbage collection threads as an environment variable: [Environment Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/environment-variables/#env-gc-threads)  
Note that by default, Julia sets this to `nthreads() ÷ 2`, so you shouldn’t need to set this explicitly.

---

<div class="post-metadata">

**Author:** ![bdas123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdas123/32/32142_2.png) [@bdas123](https://discourse.julialang.org/u/bdas123)\
**Post date:** [January 3, 2024, 3:58pm UTC](https://discourse.julialang.org/t/enable-parallel-processing-of-garbage-collections/108308/6 "2024-01-03T15:58:36Z")

</div>

More of a theoretical question, but why set this to half the number of threads? Why not the full amount of threads, if that is available?

---

<div class="post-metadata">

**Author:** ![bdas123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdas123/32/32142_2.png) [@bdas123](https://discourse.julialang.org/u/bdas123)\
**Post date:** [January 3, 2024, 4:00pm UTC](https://discourse.julialang.org/t/enable-parallel-processing-of-garbage-collections/108308/7 "2024-01-03T16:00:05Z")

</div>

Yessir! It shows `4`, which is what I wanted.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 3, 2024, 4:01pm UTC](https://discourse.julialang.org/t/enable-parallel-processing-of-garbage-collections/108308/8 "2024-01-03T16:01:07Z")

</div>

To avoid oversubscription of CPU-cores. But it’s only a reasonable heuristic. You may need to tweak this.

---

<div class="post-metadata">

**Author:** ![bdas123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdas123/32/32142_2.png) [@bdas123](https://discourse.julialang.org/u/bdas123)\
**Post date:** [January 3, 2024, 4:04pm UTC](https://discourse.julialang.org/t/enable-parallel-processing-of-garbage-collections/108308/9 "2024-01-03T16:04:35Z")

</div>

Thank you @Jeff_Emanuel, @stillyslalom, and @carstenbauer for the speedy response! I appreciate you all.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [January 3, 2024, 4:05pm UTC](https://discourse.julialang.org/t/enable-parallel-processing-of-garbage-collections/108308/10 "2024-01-03T16:05:57Z")

</div>

See this issue for some detail about slowdowns observed when setting the number of GC threads to greater than the number of CPU cores: [High (default) gcthreads count causing slowdown on Julia 1.10.0-beta2 · Issue #51044 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/51044)

---

<div class="post-metadata">

**Author:** ![bdas123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdas123/32/32142_2.png) [@bdas123](https://discourse.julialang.org/u/bdas123)\
**Post date:** [January 3, 2024, 10:14pm UTC](https://discourse.julialang.org/t/enable-parallel-processing-of-garbage-collections/108308/11 "2024-01-03T22:14:39Z")

</div>

So if I have 4 CPU Cores, the sum of my threads and gcthreads can’t exceed 4?

Also, if I have 4 CPU cores, can I have 0 threads and 4 gcthreads?

---

<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:** [January 3, 2024, 10:47pm UTC](https://discourse.julialang.org/t/enable-parallel-processing-of-garbage-collections/108308/12 "2024-01-03T22:47:05Z")

</div>

you can set both to 4. regular threads and gc threads don’t run at the same time.
