Best practices to reduce allocations in Julia

Hi all,
I have a relatively big simulation function (which has 20,000 repeated loops).
The @time results show more than 64.5G allocations.
Are there some tips to reduce the allocations?

Thank you in advance.

See Performance Tips · The Julia Language. Specifically, check for type instability and slicing as the two most likely.


Thank you very much for the advice!

1 Like

Also: Common allocation mistakes


This is helpful, thank you very much @lmiq

In my simulation I have zero allocation. Line 386ff, is the main callback function.

function residual!(res, yd, y::MVector{S, SimFloat}, s::KPS4, time) where S

I use this struct

@with_kw mutable struct KPS4{S, T, P, Q, SP} <: AbstractKiteModel

as first parameter of most functions which holds the work arrays. It is alllocated only once at the start of the program.

In most cases I use SVectors and MVectors, but only for Vectors with
less than 100 elements.

Perhaps this gives you some ideas…

1 Like

Thank you very much @ufechner7 , going to check your code.