Disabling allocations

Perhaps the manual is not clear. Check this example:

This is the code (file name here: test.jl):

struct A
  x
end

function test(n,x)
  y = Vector{A}(undef,n)
  for i in 1:n
    y[i] = A(i*x)
  end
  y
end

Run julia with:

julia --track-allocation=user

Within Julia, do:

julia> using Profile

julia> include("./test.jl")
test (generic function with 1 method)

julia> test(10,rand()); # gets compiled

julia> Profile.clear_malloc_data() # clear allocations

julia> test(10,rand());

Exit Julia, this will generate a file test.jl.XXX.mem (extension .mem), which, in this case, contains:

        -
        - struct A
        -   x
        - end
        -
        - function test(n,x)
      160   y = Vector{A}(undef,n)
        0   for i in 1:n
      160     y[i] = A(i*x)
        -   end
        0   y
        - end

Where the lines with non-zero numbers are the lines where allocations occur.

6 Likes