# Shrinking array size

**URL:** <https://discourse.julialang.org/t/shrinking-array-size/31280>\
**Category:** General Usage\
**Created:** [November 19, 2019, 6:25pm UTC](https://discourse.julialang.org/t/shrinking-array-size/31280 "2019-11-19T18:25:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [November 19, 2019, 6:25pm UTC](https://discourse.julialang.org/t/shrinking-array-size/31280/1 "2019-11-19T18:25:34Z")

</div>

I have some vectors which are initially very big to do some computation, but which shrink afterwards and will stay small for the rest of the code’s execution. Unfortunately, it seems that Julia’s underlying arrays never really shrink:

```julia
julia> x = fill(1,100);

julia> ccall(:jl_array_size, Int, (Any, UInt), x, 1)
100

julia> empty!(x)
0-element Array{Int64,1}

julia> sizehint!(x, 0)
0-element Array{Int64,1}

julia> ccall(:jl_array_size, Int, (Any, UInt), x, 1)
100

```

This code from `array.c` seems to suggest that shrinking is possible:

```nohighlight
JL_DLLEXPORT void jl_array_sizehint(jl_array_t *a, size_t sz)
{
    size_t n = jl_array_nrows(a);

    int min = a->offset + a->length;
    sz = (sz < min) ? min : sz;

    if (sz <= a->maxsize) {
        size_t dec = a->maxsize - sz;
        //if we don't save at least an eighth of maxsize then its not worth it to shrink
        if (dec < a->maxsize / 8) return;
        jl_array_shrink(a, dec);
    }
    else {
        size_t inc = sz - n;
        jl_array_grow_end(a, inc);

        a->nrows = n;
#ifdef STORE_ARRAY_LEN
        a->length = n;
#endif
    }
}

```

Unfortunately `jl_array_shrink` is unexported. I’ve been otherwise unable to see any reduction in `jl_array_size`. Am I misunderstanding something?

---

<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 19, 2019, 6:44pm UTC](https://discourse.julialang.org/t/shrinking-array-size/31280/2 "2019-11-19T18:44:57Z")

</div>

Here is the PR that implemented shrinking with `sizehint!` [https://github.com/JuliaLang/julia/pull/26201](https://github.com/JuliaLang/julia/pull/26201). But it doesn’t seem to have tests for the capacity shrinking.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [November 19, 2019, 6:58pm UTC](https://discourse.julialang.org/t/shrinking-array-size/31280/3 "2019-11-19T18:58:18Z")

</div>

Interesting, it seems that there’s a threshold around `maxsize` that I don’t understand:

```julia
julia> A = zeros(400_000);

julia> empty!(A)
0-element Array{Float64,1}

julia> sizehint!(A, 0)
0-element Array{Float64,1}

julia> ccall(:jl_array_size, Int, (Any, UInt), A, 1)
0 # worked

julia> A = zeros(400);

julia> empty!(A)
0-element Array{Float64,1}

julia> sizehint!(A, 0)
0-element Array{Float64,1}

julia> ccall(:jl_array_size, Int, (Any, UInt), A, 1)
400 # failed

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 19, 2019, 7:00pm UTC](https://discourse.julialang.org/t/shrinking-array-size/31280/4 "2019-11-19T19:00:35Z")

</div>

Inline allocated buffer cannnot be shrinked.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [November 19, 2019, 7:01pm UTC](https://discourse.julialang.org/t/shrinking-array-size/31280/5 "2019-11-19T19:01:48Z")

</div>

That makes sense. How big is that buffer?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 19, 2019, 7:18pm UTC](https://discourse.julialang.org/t/shrinking-array-size/31280/6 "2019-11-19T19:18:31Z")

</div>

It’s not a fixed size but an array with that buffer will never have that freed. It’s not something you need to worry about if you have some very big array initially.
