# How can I limit the number of CPU cores Julia has access to?

**URL:** <https://discourse.julialang.org/t/how-can-i-limit-the-number-of-cpu-cores-julia-has-access-to/93082>\
**Category:** Performance\
**Tags:** question\
**Created:** [January 17, 2023, 10:39am UTC](https://discourse.julialang.org/t/how-can-i-limit-the-number-of-cpu-cores-julia-has-access-to/93082 "2023-01-17T10:39:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashley/32/45933_2.png) [@Ashley](https://discourse.julialang.org/u/Ashley)\
**Post date:** [January 17, 2023, 10:39am UTC](https://discourse.julialang.org/t/how-can-i-limit-the-number-of-cpu-cores-julia-has-access-to/93082/1 "2023-01-17T10:39:12Z")

</div>

I am using a PBS cluster system where I request a number of CPU cores for a job. However, my Julia program seems to use more than what I allocate, causing the job to be killed by the manager!

I have tried setting `JULIA_NUM_THREADS` and `JULIA_CPU_THREADS` each to `1` to no avail - the process continues to use more than 1 CPU core, with usage reported by `top` to be 300-400%.

What else can I do to limit the CPU usage/cores of a Julia process or Julia code?

I am using Julia v1.6.1, but can update if needed.

This might not be relevant, but the process I am running uses [QSM.jl](https://github.com/kamesy/QSM.jl).

---

<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 17, 2023, 10:57am UTC](https://discourse.julialang.org/t/how-can-i-limit-the-number-of-cpu-cores-julia-has-access-to/93082/2 "2023-01-17T10:57:01Z")

</div>

Maybe try setting the number of BLAS threads explicitly: `OPENBLAS_NUM_THREADS=1` (or whatever value makes sense for your application). Also, you should be able to use `htop` to see which threads inside of the julia process are causing this.

I would have expected though that PBS sets affinity settings such that you can’t use CPU-cores outside of your allocation in the first place. But I guess this varies a lot between scheduling systems and clusters. Generally, I tend to only use nodes exclusively and manually tune everything to fully utilize the node to avoid any such node-sharing hassles.
