# Shrinking and expanding vectors

**URL:** <https://discourse.julialang.org/t/shrinking-and-expanding-vectors/7356>\
**Category:** New to Julia\
**Created:** [November 28, 2017, 12:49pm UTC](https://discourse.julialang.org/t/shrinking-and-expanding-vectors/7356 "2017-11-28T12:49:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [November 28, 2017, 12:49pm UTC](https://discourse.julialang.org/t/shrinking-and-expanding-vectors/7356/1 "2017-11-28T12:49:25Z")

</div>

I have the following scenario: a variable number of elements, let’s say from 100 to 1500, and this number fluctuates with each iteration. These elements are swapped between 2 buffers, each preallocated with space for 2000 elements. Buffers should

- allow to set their size explicitly (if number of elements \< 2000, no allocation, if bigger, grow and allocate a bigger chunk of memory).
- allow to sequentially fill them with `push!`, which affects only their `length()` property, if the size of the buffer is not exceeded.

Questions:

1. What data structure should I use for these buffers in Julia? `Vector{T}` seems to be not sufficient, but I may be wrong.
2. What `empty!()` does to let’s say `buff = Vector{E}(2000); empty!(buff)` - is all allocated memory returned to the system, or, when, after emptying, we do `push!(buff, E())` it reuses the allocated block `(2000 * sizeof(E))` of memory?

I would highly appreciate any piece of advice here.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 28, 2017, 1:00pm UTC](https://discourse.julialang.org/t/shrinking-and-expanding-vectors/7356/2 "2017-11-28T13:00:51Z")

</div>

It seems `Vector` is sufficient for this, or what is your complaint?

Allocated memory for vectors is never returned to the system until the vector itself is garbage collected. There has been discussions about adding such a feature though, since some people have desired it. The memory is reused when pushing after a resize to zero length.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 28, 2017, 1:13pm UTC](https://discourse.julialang.org/t/shrinking-and-expanding-vectors/7356/3 "2017-11-28T13:13:10Z")

</div>

AFAIK something like

```julia
v = Vector{T}()
sizehint!(v, 2000)
push!(v, something)

```

should be totally fine. Julia vectors are very clever under the hood, the allocated memory may exceed the “apparent” size `length(v)`, and is over-expanded when needed, with chunks of increasing size.

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [November 28, 2017, 8:14pm UTC](https://discourse.julialang.org/t/shrinking-and-expanding-vectors/7356/4 "2017-11-28T20:14:02Z")

</div>

Thank you for `sizehint!`, overlooked it in the documentation!

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [November 28, 2017, 8:16pm UTC](https://discourse.julialang.org/t/shrinking-and-expanding-vectors/7356/5 "2017-11-28T20:16:52Z")

</div>

> [@kristoffer.carlsson](#):
>
> Allocated memory for vectors is never returned to the system until the vector itself is garbage collected

This solves my doubts, in this case, alongside with `sizehint!`, everything should work just fine. Thanks!
