# Does @allocated not work with multithreading?

**URL:** https://discourse.julialang.org/t/does-allocated-not-work-with-multithreading/59354
**Category:** Performance
**Tags:** bug, multithreading, memory-allocation
**Created:** [April 15, 2021, 4:14pm UTC](https://discourse.julialang.org/t/does-allocated-not-work-with-multithreading/59354 "2021-04-15T16:14:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nmayhall-vt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nmayhall-vt/32/12587_2.png) [@nmayhall-vt](https://discourse.julialang.org/u/nmayhall-vt)
#### Post date: [April 15, 2021, 4:14pm UTC](https://discourse.julialang.org/t/does-allocated-not-work-with-multithreading/59354/1 "2021-04-15T16:14:50Z")

</div>

I’ve been trying to better undertstand how to find allocations in my code, and I stumbled on this simple MWE which either exposes some limitation of @allocated, or (more likely) a limitation of my understanding.

```julia
function test() 
    a = @allocated begin
    end
    if a>0 
        println(a)  
        error(" Found an allocation?")
    end 
end

```

With nothing in the begin/end block, no allocations should be done, and so the function should just finish without an error. This is the case if I just run it on a single thread.

```julia
for _ in 1:10000
    test()
end

```

But now doing the same thing in a threaded loop behaves differently (it returns the error message):

```julia
Threads.@threads for _ in 1:10000
    test()
end

```

Am I doing something dumb here?

I’m using Julia Version 1.5.2 (2020-09-23):

```nohighlight
Commit 539f3ce943 (2020-09-23 23:17 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin18.7.0)
  CPU: Intel(R) Core(TM) i7-8700B CPU @ 3.20GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 12

```

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [April 16, 2021, 4:18am UTC](https://discourse.julialang.org/t/does-allocated-not-work-with-multithreading/59354/2 "2021-04-16T04:18:21Z")

</div>

Yes, multithreading causes allocations. The allocations are essentially fundamental to being able to coordinate dataflow and communication between threads.

---

<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: [April 16, 2021, 4:37am UTC](https://discourse.julialang.org/t/does-allocated-not-work-with-multithreading/59354/3 "2021-04-16T04:37:38Z")

</div>

I wouldn’t say they’re essential. BLAS, for example, doesn’t allocate.

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [April 16, 2021, 5:12am UTC](https://discourse.julialang.org/t/does-allocated-not-work-with-multithreading/59354/4 "2021-04-16T05:12:38Z")

</div>

Well, I think it’s fair to say BLAS does not coordinate (arbitrary) dataflow and communication 🙂

> [@nmayhall-vt](#):
>
> exposes some limitation of @allocated

Yes, this is the case. It’s not clearly documented, but `@allocated` cannot be used concurrently. It uses “a global counter” \[1\] to detect allocations. That is to say, with

```julia
@sync begin
    @spawn do_some_allocation()
    x = @allocated nothing
end

```

`x` can, in principle, include some allocations in `do_some_allocation`.

* * *

1. More specifically this is a set of counters local to OS thread but shared across executions. But at the level of Julia interface, it acts as if a single counter [by combining the set of counters](https://github.com/JuliaLang/julia/blob/29d5158d27ddc3983ae2e373c4cd05569b9ead6c/src/gc.c#L1055-L1069).
