# Memory allocation due to assignment in a fixed size mutable array

**URL:** <https://discourse.julialang.org/t/memory-allocation-due-to-assignment-in-a-fixed-size-mutable-array/12759>\
**Category:** General Usage\
**Created:** [July 31, 2018, 3:38am UTC](https://discourse.julialang.org/t/memory-allocation-due-to-assignment-in-a-fixed-size-mutable-array/12759 "2018-07-31T03:38:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Snir\_Gazit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/snir_gazit/32/11027_2.png) [@Snir\_Gazit](https://discourse.julialang.org/u/Snir_Gazit)\
**Post date:** [July 31, 2018, 3:38am UTC](https://discourse.julialang.org/t/memory-allocation-due-to-assignment-in-a-fixed-size-mutable-array/12759/1 "2018-07-31T03:38:59Z")

</div>

I am writing a function that stores locally a temporary set of values saved for a later computation. During “compile” time I know the size of the array. To avoid memory allocation, I am using a mutable array from the StaticArray package. See example code below that illustrates a random assignment ( in the actual code, the assignment involves a more elaborate computation).

```julia
function test1()
    a=MVector{6,Float64}()
    a[4]=rand()
    a[1]=rand()
end
function test2()
    for i in 1:10000
        test1()
    end
end

```

Timing the above results in memory allocation.

```julia
@time test2()
  0.000309 seconds (10.00 k allocations: 625.156 KiB)

```

Is that expected? Since I know the array size at “compile time” should not there be a way to eliminate this memory cost? I am looking for an object that behaves like `std::array` from c++. What is the “Julian” way?

Thanks!

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [July 31, 2018, 3:53am UTC](https://discourse.julialang.org/t/memory-allocation-due-to-assignment-in-a-fixed-size-mutable-array/12759/2 "2018-07-31T03:53:18Z")

</div>

This doesn’t allocate on latest Julia nightly (after fixing the deprecation warning).

On 0.6, it’s not the assignment that allocates, it’s creating the `MVector`. The rule of thumb for 0.6 is that creating non-`isbits` structs (structs that are either mutable or contain references to other values) _always_ allocates on the heap. The upcoming Julia release is much better at eliminating such allocations.
