# @benchmark resize! : no memory allocation?

**URL:** <https://discourse.julialang.org/t/benchmark-resize-no-memory-allocation/57112>\
**Category:** General Usage\
**Tags:** benchmarktools\
**Created:** [March 14, 2021, 5:55am UTC](https://discourse.julialang.org/t/benchmark-resize-no-memory-allocation/57112 "2021-03-14T05:55:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)\
**Post date:** [March 14, 2021, 5:55am UTC](https://discourse.julialang.org/t/benchmark-resize-no-memory-allocation/57112/1 "2021-03-14T05:55:58Z")

</div>

```julia
julia> using BenchmarkTools

julia> x = zeros(10);

julia> @benchmark resize!($x, 10000)
BenchmarkTools.Trial:
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 2.499 ns (0.00% GC)
  median time: 2.601 ns (0.00% GC)
  mean time: 2.720 ns (0.00% GC)
  maximum time: 23.999 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1000

julia> length(x)
10000

```

In the above example, the size of `x` has been increased from `10` to `10000`. However, `@benchmark` reports no memory allocation. Any explanation?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 14, 2021, 5:58am UTC](https://discourse.julialang.org/t/benchmark-resize-no-memory-allocation/57112/2 "2021-03-14T05:58:14Z")

</div>

I don’t think you can increase size with `resize!` only decrease. I would have expected an error, though.

---

<div class="post-metadata">

**Author:** ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)\
**Post date:** [March 14, 2021, 6:00am UTC](https://discourse.julialang.org/t/benchmark-resize-no-memory-allocation/57112/3 "2021-03-14T06:00:29Z")

</div>

We can increase the size. Check the help:

```julia
  resize!(a::Vector, n::Integer) -> Vector

  Resize a to contain n elements. If n is smaller than the current collection length, the first n elements
  will be retained. If n is larger, the new elements are not guaranteed to be initialized.

```

---

<div class="post-metadata">

**Author:** ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)\
**Post date:** [March 14, 2021, 6:31am UTC](https://discourse.julialang.org/t/benchmark-resize-no-memory-allocation/57112/4 "2021-03-14T06:31:05Z")

</div>

`BenchmarkTools.jl` runs the function multiple times to give statistical results, in this case 10,000 samples. Only the first sample actually resized the array.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 14, 2021, 6:32am UTC](https://discourse.julialang.org/t/benchmark-resize-no-memory-allocation/57112/5 "2021-03-14T06:32:37Z")

</div>

You’re calling `resize!` on the _same_ value over and over, so it has no effect at all after the very first iteration. Thus the number of allocations per iteration (after the first) is exactly zero.

The BenchmarkTools manual explains how to avoid this situation here: [https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#setup-and-teardown-phases](https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#setup-and-teardown-phases)

e.g.

```julia
julia> @btime resize!(x, 10000) setup=(x = zeros(10)) evals=1
  254.000 ns (1 allocation: 78.19 KiB)

```

---

<div class="post-metadata">

**Author:** ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)\
**Post date:** [March 14, 2021, 6:49am UTC](https://discourse.julialang.org/t/benchmark-resize-no-memory-allocation/57112/6 "2021-03-14T06:49:55Z")

</div>

Thanks. You are correct. I want to verify the following hypothesis:

```julia
julia> @btime resize!(x, 10000) setup=(x=zeros(10000); resize!(x, 100)) evals=1;
  0.001 ns (0 allocations: 0 bytes)

```

After the first `resize!(x, 100)` inside setup, the memory originally allocated by `x=zeros(10000)` is not freed actually, right? I want to figure out whether Julia has the same behaviour as C++ std::vector in this case.
