Shrinking and expanding vectors

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.


  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.

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.

AFAIK something like

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.


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

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