# Why does Julia allocate memory when I already pre-allocated?

**URL:** https://discourse.julialang.org/t/why-does-julia-allocate-memory-when-i-already-pre-allocated/65223
**Category:** Performance
**Created:** [July 24, 2021, 8:43am UTC](https://discourse.julialang.org/t/why-does-julia-allocate-memory-when-i-already-pre-allocated/65223 "2021-07-24T08:43:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [July 24, 2021, 8:43am UTC](https://discourse.julialang.org/t/why-does-julia-allocate-memory-when-i-already-pre-allocated/65223/1 "2021-07-24T08:43:00Z")

</div>

Look at the code here.

```julia
julia> results = Vector{Int}(undef, 10^7);

julia> @time for i in 1:10^7
           results[i] = i
       end
  0.315568 seconds (20.00 M allocations: 305.160 MiB, 5.86% gc time)

```

Why is there memory allocation and GC run, when the vector was allocated beforehand?

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [July 24, 2021, 8:49am UTC](https://discourse.julialang.org/t/why-does-julia-allocate-memory-when-i-already-pre-allocated/65223/2 "2021-07-24T08:49:23Z")

</div>

`results` is global variable.

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [July 24, 2021, 9:00am UTC](https://discourse.julialang.org/t/why-does-julia-allocate-memory-when-i-already-pre-allocated/65223/3 "2021-07-24T09:00:54Z")

</div>

Do you mean that when an array is declared in global scope, I actually get an array of pointers to heap-allocated objects?

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [July 24, 2021, 9:07am UTC](https://discourse.julialang.org/t/why-does-julia-allocate-memory-when-i-already-pre-allocated/65223/4 "2021-07-24T09:07:08Z")

</div>

More likely there’s a bit of boxing and unboxing going on in each iteration of the loop while executing an unspecialized `setindex!`.

If you place the loop in a function with `results` as an argument or make `results` a const global you get more efficient code.

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [July 24, 2021, 9:20am UTC](https://discourse.julialang.org/t/why-does-julia-allocate-memory-when-i-already-pre-allocated/65223/5 "2021-07-24T09:20:19Z")

</div>

Thanks for the tips. I fixed the problem. I just had to add `const` before declaring the array `results`, and the memory allocation disappeared. I was aware that non-`const` globals are bad, but didn’t realize it would affect code as simple as this.

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [July 24, 2021, 9:25am UTC](https://discourse.julialang.org/t/why-does-julia-allocate-memory-when-i-already-pre-allocated/65223/6 "2021-07-24T09:25:16Z")

</div>

The simplicity of code matters not. If variable is global it allocates, so you should either use `const` or wrap in `let` block or wrap in a function. You can read more here: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables)
