Can I force a vector to release its memory?


#1
a = zeros(4000000000)
resize!(a, 0)
sizehint!(a, 0)
b = zeros(4000000000)
resize!(b, 0)
sizehint!(b, 0)
c = zeros(4000000000)
while true
end

I ran into OutOfMemoryError with the above program. My computer has 64GiB of RAM. According to https://github.com/JuliaLang/julia/pull/26201, I was hoping that when I actually need to create c, a's and b's memory have been released instead of running out of memory. Calling gc() doesn’t help. Is there anything I can do to force Julia to release a's and b's memory? Thanks!


#2

For what it’s worth, my computer also has 64 gigs, and was able to create c. (Although I didn’t check the swap, I didn’t notice creating c being any slower than the first two).

Did you try?

a = zeros(4000000000);
a = zeros(0);
b = zeros(4000000000);
b = zeros(0);
c = zeros(4000000000);

#3

Oh, this does work. I check that it’s only consuming about 30GiB in the end which is expected when a's and b's memory is released. Thanks!


#4

But I still wonder when sizehint! actually releases those memory.


#5

IIRC, sizehint! is only used for growing capacity. It will not reduce capacity.

Oops, my info was out of date. Per https://github.com/JuliaLang/julia/pull/26201, it appears that sizehint! can now shrink capacity.


#6

Okay, so sizehint! should release memory on master, but not 0.6.2.