# Can I force a vector to release its memory?

**URL:** <https://discourse.julialang.org/t/can-i-force-a-vector-to-release-its-memory/11158>\
**Category:** General Usage\
**Created:** [May 26, 2018, 4:49am UTC](https://discourse.julialang.org/t/can-i-force-a-vector-to-release-its-memory/11158 "2018-05-26T04:49:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [May 26, 2018, 4:49am UTC](https://discourse.julialang.org/t/can-i-force-a-vector-to-release-its-memory/11158/1 "2018-05-26T04:49:27Z")

</div>

```julia
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](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!

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [May 26, 2018, 5:02am UTC](https://discourse.julialang.org/t/can-i-force-a-vector-to-release-its-memory/11158/2 "2018-05-26T05:02:15Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [May 26, 2018, 5:05am UTC](https://discourse.julialang.org/t/can-i-force-a-vector-to-release-its-memory/11158/3 "2018-05-26T05:05:11Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [May 26, 2018, 5:07am UTC](https://discourse.julialang.org/t/can-i-force-a-vector-to-release-its-memory/11158/4 "2018-05-26T05:07:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [May 26, 2018, 5:10am UTC](https://discourse.julialang.org/t/can-i-force-a-vector-to-release-its-memory/11158/5 "2018-05-26T05:10:17Z")

</div>

~~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](https://github.com/JuliaLang/julia/pull/26201), it appears that `sizehint!` can now shrink capacity.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [May 26, 2018, 5:14am UTC](https://discourse.julialang.org/t/can-i-force-a-vector-to-release-its-memory/11158/6 "2018-05-26T05:14:15Z")

</div>

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