# Free last part of a vector

**URL:** <https://discourse.julialang.org/t/free-last-part-of-a-vector/88793>\
**Category:** Performance\
**Created:** [October 16, 2022, 5:12am UTC](https://discourse.julialang.org/t/free-last-part-of-a-vector/88793 "2022-10-16T05:12:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Post date:** [October 16, 2022, 5:12am UTC](https://discourse.julialang.org/t/free-last-part-of-a-vector/88793/1 "2022-10-16T05:12:02Z")

</div>

I have allocated a long vector, and filled it to nearly the end.  
I don’t need the last few, remaining elements. That memory can be freed.

Is there a way to ‘chop off’ the last part of the vector – without creating a `view`, or copying to a new vector (by slicing, `x[1:i]`) ?

(Context is, I’m simulating a Poisson process within a fixed time interval:

```julia
using Distributions

function gen_Poisson_spikes(rate, duration)
    N_distr = Poisson(rate * duration)
    max_N = cquantile(N_distr, 1e-14) # Cannot take a much smaller chance than ~1e-15:
                                       # https://github.com/JuliaStats/Rmath-julia/blob/master/src/qpois.c#L86
    spikes = Vector{Float64}(undef, max_N)
    ISI_distr = Exponential(inv(rate))
    i = 0
    t = zero(duration)
    while t ≤ duration
        i += 1
        spikes[i] = (t += rand(ISI_distr))
    end
    spikes[1:i]
end

```

For rate = 100 Hz and duration = 10 minutes, the typical N ≈ 60000, and `max_N` (at p \< 1e-14) is 61855.  
i.e. we don’t need the last ~1855, uninitialized elements of `spikes`.  
)

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [October 16, 2022, 5:48am UTC](https://discourse.julialang.org/t/free-last-part-of-a-vector/88793/2 "2022-10-16T05:48:13Z")

</div>

try to check if the `resize!()` function is right for you
