# Help to reduce number of allocations

**URL:** <https://discourse.julialang.org/t/help-to-reduce-number-of-allocations/19308>\
**Category:** General Usage\
**Created:** [January 5, 2019, 4:46pm UTC](https://discourse.julialang.org/t/help-to-reduce-number-of-allocations/19308 "2019-01-05T16:46:47Z")\
**Posts on this page:** 1\
**Showing post:** 17

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [January 5, 2019, 10:38pm UTC](https://discourse.julialang.org/t/help-to-reduce-number-of-allocations/19308/17 "2019-01-05T22:38:11Z")

</div>

There’s also Julia’s built-in performance and memory profiler, if you add this at the end of your script:

```julia
using Profile
teste() # run w/o profiling first to ensure compiled & warmed up
@profile teste()
Profile.print(maxdepth=6)

```

You’ll get something like the below (trimmed a bit):

```julia
361 ...usr/share/julia/stdlib/v1.0/Profile/src/Profile.jl:25; top-level scope
  14 allocation_test.jl:9; teste()
    13 ./arraymath.jl:47; +(::Array{Float64,2}, ::Array{Float64,2})
    1 .../usr/share/julia/stdlib/v1.0/Random/src/Random.jl:241; rand
  321 allocation_test.jl:10; teste()
    318 ...are/julia/stdlib/v1.0/LinearAlgebra/src/dense.jl:1296; pinv(::Array{Float64,2})
    3 .../stdlib/v1.0/LinearAlgebra/src/uniformscaling.jl:90; +(::UniformScaling{Bool}, ::Array{Float64,2})
  22 allocation_test:11; teste()
    2 ./arraymath.jl:47; +(::Array{Float64,2}, ::Array{Float64,2})
    6 ./operators.jl:502; *(::Array{Float64,2}, ::Array{Float64,2}, ::Adjoint{Float64,Array{Float64,2}})
    5 ./operators.jl:502; *
    5 ...a/stdlib/v1.0/LinearAlgebra/src/uniformscaling.jl:124; -(::UniformScaling{Bool}, ::Array{Float64,2})

```

I find this tool very useful. Increase the `maxdepth` for more detail. There’s also a graphical interface from within Atom if you prefer that.

Then there’s memory allocation tracking. If you start Julia with the flag `--track-allocation=user` and put this at the end of your script:

```julia
using Profile
teste() # run w/o profiling first to ensure compiled & warmed up
Profile.clear_malloc_data()
teste()

```

Then quit Julia, and locate the newly created `.mem` file. It will have annotated each line with how much it allocated:

```julia
        - function teste()
    50160 P = Vector{Matrix}(undef, 6000)
        -
     2752 P[1] = zeros(18,18)
        -
        0 @inbounds for k = 2:6000
 33018496 Pp = P[k-1] + rand(18,18)
        0 K = pinv(I + Pp)
 16597072 P[k] = (I - K)*Pp*(I - K)' + K*I*K'
        - end
        -
        0 P
        - end

```

Unfortunately, as you can see above, it’s not very accurate since first of all it shows 0 bytes allocated for the `pinv` line, and second the total number of bytes allocated is way off (it should be ~357 MB). Not sure what’s going on there, I have the impression that this worked well in Julia 0.6 but hasn’t quite worked since. When/if this works, it is an awesome tool. Apart from the obvious use case of seeing what allocates, you can also use it to detect type instabilities.

---

_[View the full topic](https://discourse.julialang.org/t/help-to-reduce-number-of-allocations/19308)._
