How to release the extra buffer after insert! an array?

I found that julia allocates a lot when insert! an array for the first time. But for the second or subsequent insert! operations, the memory allocation becomes 0. I guess that the initial insert! pre-allocates memory to enhance array modification speed (am I correct?). This allocated memory is often quite substantial, which can be burdensome for large number of arrays. Considering that the modification frequency of each array is not high, is there a way to release this pre-allocated memory?

sizehint! should be fine. If you want extra control, maybe a custom array type would be better.

1 Like

There seems to be a minimum amount of pre-allocated memory in Julia, as sizehint! will pre-allocate at least this amount of memory, even if the amount I request is lower than this value. See:

a = collect(1:1000)
@allocated sizehint!(a,1001)

returns 17280. An interesting fact is that insert! also allocates the same:

b = collect(1:1000)
@allocated insert!(b,2,1)
  1. try sizehint! on an empty array
  2. try the upcoming release (v1.11), as possibly some things changed

I hope to preserve the dynamic nature of arrays, so sizehint! an empty array does not meet the requirements. Perhaps there will be other methods? Anyway, looking forward to v1.11!

Yes that’s correct. It’s a common technique for amortizing the cost of resizing a dynamic array, since resizing must eventually copy the data in the array around. IIRC the default is to double the capacity (=reserved memory) whenever the array needs to grow its backing memory.

If you allocate an empty array, IIRC it reserves a capacity of 32 elements. Calling sizehint! on that is about as efficient as you can get, short of allocating the array with the correct capacity in the first place and calling empty! afterwards. The reserved capacity should be retained in that case.

I’m not sure what you mean here - why not? sizehint! is all about the reserved capacity, not the exposed size. You can have an array with a capacity of let’s say 5000 elements, and only expose 500 of those.


Thanks for your explaination. When I mentioned that sizehint! doesn’t meet my needs, it’s because there are a large number of arrays in the program, and to avoid out of memory errors, I want the pre-allocated memory to be released immediately after resizing the arrays. I think this could be achieved by copying the elements from the original array to a newly created array with precisely pre-allocated size, but this approach may lead to low efficiency (maybe that’s the best I can get?).

1 Like

It depends on your use case. If you know the size of array in advance, you can use static array.
However, if you want the most compact possible array, then it’s difficult. Even C malloc is allowed to allocate more memory than you actually requested. Many structs are also padded.

Perhaps using vcat can help? But honestly, there is no guarantee. Julia array is allowed to be larger than its determined size.

1 Like

I see… Thanks a lot!