# Too many allocations?

**URL:** https://discourse.julialang.org/t/too-many-allocations/55478
**Category:** Performance
**Created:** [February 17, 2021, 5:31pm UTC](https://discourse.julialang.org/t/too-many-allocations/55478 "2021-02-17T17:31:19Z")
**Posts on this page:** 1
**Showing post:** 16

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [February 17, 2021, 7:25pm UTC](https://discourse.julialang.org/t/too-many-allocations/55478/16 "2021-02-17T19:25:09Z")

</div>

If you do these changes, you reduce even further the allocations:

```julia
    rtmp = zeros(2) # preallocate a temporary vector here
    for i=1:N
        k1 = pend.dt .* getDerivs(pend,r,t)
        rtmp .= r .+ 0.5 .* k1 # compute it here
        k2 = pend.dt .* getDerivs(pend,rtmp,t + 1/2 * pend.dt)
        r .+= k2 # add the dot

```

```julia
julia> @time saveTheta,saveOmega = RK4(myPendulum,poincare=true)
  0.977168 seconds (19 allocations: 488.308 MiB, 4.39% gc time)

```

(now the loop does not allocate anything).

Some tips here on how to find the allocations: [Disabling allocations](https://discourse.julialang.org/t/disabling-allocations/51028)

My impression here is: loops that perform critical tasks should only allocate if one is clearly aware of the reason of the allocation. Otherwise it is a good idea to search where the allocation is and try to solve it. Of course, if the execution time is _still_ a problem at all.

---

_[View the full topic](https://discourse.julialang.org/t/too-many-allocations/55478)._
