# Allocations in @threads loop

**URL:** <https://discourse.julialang.org/t/allocations-in-threads-loop/3814>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [May 19, 2017, 9:56pm UTC](https://discourse.julialang.org/t/allocations-in-threads-loop/3814 "2017-05-19T21:56:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcovalt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcovalt/32/1722_2.png) [@mcovalt](https://discourse.julialang.org/u/mcovalt)\
**Post date:** [May 19, 2017, 9:56pm UTC](https://discourse.julialang.org/t/allocations-in-threads-loop/3814/1 "2017-05-19T21:56:18Z")

</div>

With the following code:

```julia
function thread_test(x::Vector{Float64}, y::Vector{Float64}, z::Vector{Float64}, n::Int64)
    for j = 1:10000
        a::Vector{Int64} = rand(1:n, 128)
        b::Vector{Int64} = rand(1:n, 128)
        c::Vector{Int64} = rand(1:n, 128)
        Threads.@threads for i::Int64 = 1:128
            y[a[i]] += x[b[i]]*z[c[i]]
        end
    end
end

function no_thread_test(x::Vector{Float64}, y::Vector{Float64}, z::Vector{Float64}, n::Int64)
    for j = 1:10000
        a::Vector{Int64} = rand(1:n, 128)
        b::Vector{Int64} = rand(1:n, 128)
        c::Vector{Int64} = rand(1:n, 128)
        for i::Int64 = 1:128
            y[a[i]] += x[b[i]]*z[c[i]]
        end
    end
end

function test()
    n = 1000
    x = rand(n)
    y = rand(n)
    z = rand(n)
    thread_test(x,y,z,n)
    no_thread_test(x,y,z,n)
    @time thread_test(x,y,z,n)
    @time no_thread_test(x,y,z,n)
end

test()

```

I get the following output

```julia
0.177082 seconds (1.02 M allocations: 65.100 MiB, 23.14% gc time)
0.059489 seconds (30.00 k allocations: 33.417 MiB, 1.12% gc time)

```

Where are all these allocations coming from for the threaded case?

Additionally, I noticed that if I didn’t explicitly type `i`, allocations increased further:

```julia
0.344228 seconds (3.45 M allocations: 94.762 MiB)

```

Why is this?

---

<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:** [May 19, 2017, 11:29pm UTC](https://discourse.julialang.org/t/allocations-in-threads-loop/3814/2 "2017-05-19T23:29:47Z")

</div>

[https://github.com/JuliaLang/julia/issues/15276](https://github.com/JuliaLang/julia/issues/15276)

---

<div class="post-metadata">

**Author:** ![mcovalt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcovalt/32/1722_2.png) [@mcovalt](https://discourse.julialang.org/u/mcovalt)\
**Post date:** [May 20, 2017, 12:59am UTC](https://discourse.julialang.org/t/allocations-in-threads-loop/3814/3 "2017-05-20T00:59:19Z")

</div>

Thanks for this. I’m trying to wrap my head around it now. Is this only applicable to explicitly typing `i` in my example, or is this the cause to all the allocations?

---

<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:** [May 20, 2017, 2:28am UTC](https://discourse.julialang.org/t/allocations-in-threads-loop/3814/4 "2017-05-20T02:28:40Z")

</div>

Everything
