# Why does this function allocate memory?

**URL:** <https://discourse.julialang.org/t/why-does-this-function-allocate-memory/90624>\
**Category:** Performance\
**Created:** [November 22, 2022, 4:24am UTC](https://discourse.julialang.org/t/why-does-this-function-allocate-memory/90624 "2022-11-22T04:24:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [November 22, 2022, 4:24am UTC](https://discourse.julialang.org/t/why-does-this-function-allocate-memory/90624/1 "2022-11-22T04:24:23Z")

</div>

The [Performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/) page in the documentation gives the following example of a function that allocates memory unexpectedly:

```julia
julia> x = rand(1000);

julia> function sum_global()
           s = 0.0
           for i in x
               s += i
           end
           return s
       end;

```

The page explains that this can be fixed by providing the array as an argument instead, but it doesn’t explain what’s causing the problem in the first place.

Why does this happen? For what purpose is memory being allocated here? Why does passing x as an argument fix the problem? Is there another way to fix it without passing the array as an argument?

I’m being bitten by something similar in my own code - a function is allocating memory that really doesn’t look like it should be allocating memory. I can’t work out what I’m doing wrong so I would like to have a better general understanding of what causes memory to be allocated in this type of situation.

---

<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:** [November 22, 2022, 4:40am UTC](https://discourse.julialang.org/t/why-does-this-function-allocate-memory/90624/2 "2022-11-22T04:40:56Z")

</div>

the problem is type instability. the compiler can’t figure out what type `i` is, so it has to box it. this means it can’t figure out what method of + gets used, so it has to do a runtime dispatch. them it doesn’t know what type that returns so it has to box s also.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 22, 2022, 7:38am UTC](https://discourse.julialang.org/t/why-does-this-function-allocate-memory/90624/3 "2022-11-22T07:38:32Z")

</div>

> [@Nathaniel](#):
>
> Is there another way to fix it without passing the array as an argument?

```julia
const x = rand(1000)

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [November 22, 2022, 7:49am UTC](https://discourse.julialang.org/t/why-does-this-function-allocate-memory/90624/4 "2022-11-22T07:49:38Z")

</div>

> [@Nathaniel](#):
>
> Is there another way to fix it without passing the array as an argument?

```julia
x::Vector{Float64} = rand(1000)

```

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [November 22, 2022, 8:19am UTC](https://discourse.julialang.org/t/why-does-this-function-allocate-memory/90624/5 "2022-11-22T08:19:43Z")

</div>

> [@uniment](#):
>
> ```julia
> x::Vector{Float64} = rand(1000)
> 
> ```

Note that this require Julia \>= v1.8

---

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [November 22, 2022, 8:26am UTC](https://discourse.julialang.org/t/why-does-this-function-allocate-memory/90624/6 "2022-11-22T08:26:12Z")

</div>

Thanks, these responses are helpful. (But they make it clear that the problem in my code is something different. I’ll try to isolate the issue and post separately if I don’t manage to fix it.)

---

<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:** [November 22, 2022, 9:18am UTC](https://discourse.julialang.org/t/why-does-this-function-allocate-memory/90624/7 "2022-11-22T09:18:17Z")

</div>

A somewhat common source of mysterious allocations is the infamous [issue 15276](https://github.com/JuliaLang/julia/issues/15276). It might be worth checking up in case you are using closures.
