# Question on allocations for push pop of arrays

**URL:** <https://discourse.julialang.org/t/question-on-allocations-for-push-pop-of-arrays/52593>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 29, 2020, 10:30pm UTC](https://discourse.julialang.org/t/question-on-allocations-for-push-pop-of-arrays/52593 "2020-12-29T22:30:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![asyrov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asyrov/32/13578_2.png) [@asyrov](https://discourse.julialang.org/u/asyrov)\
**Post date:** [December 29, 2020, 10:30pm UTC](https://discourse.julialang.org/t/question-on-allocations-for-push-pop-of-arrays/52593/1 "2020-12-29T22:30:24Z")

</div>

Need do confirm that if we do push! to array of a single element it reallocates with just one extra space for new element (in opposite to, say, preallocate 2 times more elements for better average performance).

Thanks!

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [December 29, 2020, 10:37pm UTC](https://discourse.julialang.org/t/question-on-allocations-for-push-pop-of-arrays/52593/2 "2020-12-29T22:37:16Z")

</div>

Here’s how you can check:

```julia
using Plots
list = Int[]
plot([@allocated(push!(list, i)) for i in 1:1000], xaxis=:log10)

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [December 29, 2020, 10:40pm UTC](https://discourse.julialang.org/t/question-on-allocations-for-push-pop-of-arrays/52593/3 "2020-12-29T22:40:22Z")

</div>

To answer your question, you are probably looking for `sizehint!`, which lets you control how much extra space is preallocated.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 29, 2020, 10:54pm UTC](https://discourse.julialang.org/t/question-on-allocations-for-push-pop-of-arrays/52593/4 "2020-12-29T22:54:20Z")

</div>

No, the allocation grows geometrically, by factors of 2, so that pushing n times does O(n) work.

---

<div class="post-metadata">

**Author:** ![asyrov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asyrov/32/13578_2.png) [@asyrov](https://discourse.julialang.org/u/asyrov)\
**Post date:** [December 29, 2020, 10:56pm UTC](https://discourse.julialang.org/t/question-on-allocations-for-push-pop-of-arrays/52593/5 "2020-12-29T22:56:08Z")

</div>

So, I ran the following:

```julia
lst = Int64[]
size = 100000
plot([@allocated(push!(lst, i)) for i in 1:size])
plot([@allocated(pop!(lst)) for i in 1:size])

```

This outputs the following 2 plots. First appear to be confirming that capacity of array doubles, which is nice.

 ![Screenshot from 2020-12-29 14-51-30](https://global.discourse-cdn.com/julialang/original/3X/3/d/3d8e4495482e32e73c798bace1595970dc260526.png)

This is plot for when we do pop. I expected to also see relocation, but three are none. Can anyone help to explain?

 ![Screenshot from 2020-12-29 14-52-25](https://global.discourse-cdn.com/julialang/original/3X/7/7/7726f712f8256d0ab678b01937d49be3e4b761e8.png)

---

<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:** [December 29, 2020, 11:37pm UTC](https://discourse.julialang.org/t/question-on-allocations-for-push-pop-of-arrays/52593/6 "2020-12-29T23:37:02Z")

</div>

There aren’t allocations, just frees. Since you are shrinking, you only have to free the part of memory you aren’t using any more. You never need to reallocate anything new.
