# Understanding Vector allocation

**URL:** <https://discourse.julialang.org/t/understanding-vector-allocation/76350>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [February 13, 2022, 12:26pm UTC](https://discourse.julialang.org/t/understanding-vector-allocation/76350 "2022-02-13T12:26:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dharik\_Arsath](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dharik_arsath/32/27461_2.png) [@Dharik\_Arsath](https://discourse.julialang.org/u/Dharik_Arsath)\
**Post date:** [February 13, 2022, 12:26pm UTC](https://discourse.julialang.org/t/understanding-vector-allocation/76350/1 "2022-02-13T12:26:51Z")

</div>

I am trying to scale vector of length N into range of 0 to 1. sometimes the allocation is 1 and sometimes allocation becomes 2. I cannot understand why that is happening.

if x is length(1000) 1 allocation happening for output  
if x is length 10,000 2 allocation happening.  
why? may be that is how vector works?

sample\_code:

```julia
function scale(X::Vector)::Vector
    scale_out = Vector{Float32}(undef, size(X,1))
    # Applying formula (x - mean) / std_dev
    return scale_out

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 16, 2022, 6:03pm UTC](https://discourse.julialang.org/t/understanding-vector-allocation/76350/2 "2022-02-16T18:03:47Z")

</div>

Can you give a MWE?

---

<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:** [February 16, 2022, 6:19pm UTC](https://discourse.julialang.org/t/understanding-vector-allocation/76350/3 "2022-02-16T18:19:15Z")

</div>

this is basically caused by is pooling smaller allocations together, but asking the OS directly for larger ones. you don’t need to worry about it.

---

<div class="post-metadata">

**Author:** ![Dharik\_Arsath](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dharik_arsath/32/27461_2.png) [@Dharik\_Arsath](https://discourse.julialang.org/u/Dharik_Arsath)\
**Post date:** [February 16, 2022, 6:48pm UTC](https://discourse.julialang.org/t/understanding-vector-allocation/76350/4 "2022-02-16T18:48:02Z")

</div>

until 2048 it gets 1 allocation, after 2048 it goes to 2 allocation. I have not checked further…

---

<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:** [February 16, 2022, 6:48pm UTC](https://discourse.julialang.org/t/understanding-vector-allocation/76350/5 "2022-02-16T18:48:46Z")

</div>

It shouldn’t ever be more than 2.

---

<div class="post-metadata">

**Author:** ![Dharik\_Arsath](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dharik_arsath/32/27461_2.png) [@Dharik\_Arsath](https://discourse.julialang.org/u/Dharik_Arsath)\
**Post date:** [February 16, 2022, 6:50pm UTC](https://discourse.julialang.org/t/understanding-vector-allocation/76350/6 "2022-02-16T18:50:51Z")

</div>

if I do vector{float64}(undef, size(X,1)) will this be stack allocated or heap allocated??? I have read in cpp that std::array is stack allocated but I can’t find stack allocated array in julia except staticarrays.jl which is suitable only for small arrays(quoted in their github)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 16, 2022, 6:54pm UTC](https://discourse.julialang.org/t/understanding-vector-allocation/76350/7 "2022-02-16T18:54:30Z")

</div>

Heap allocated. If you are using large arrays, probably you want preallocation.
