Incorrect memory allocation in Julia v0.5

I’m repeating what we already stated, but you’ll have a much higher chance to get somebody to look seriously at your problem if you make the effort of simplifying it as much as possible. At the very least, you need to separate it from the rest of the package so that it can be reproduced from a single independent file. If the issue is so obvious (as you said), it should be pretty easy to reduce.

5 Likes

@nalimilan you can find below an even more simplified example. This thread is only concerned with a single function called boykov_kolmogorov_cut that takes two arrays and a direction and computes something that I care about.

using ImageQuilting: boykov_kolmogorov_cut

A = rand(30,30,1)
B = rand(30,30,1)
boykov_kolmogorov_cut(A, B, :x)
Profile.clear_malloc_data()
boykov_kolmogorov_cut(A, B, :x)

If I run julia --track-allocation=user --compilecache=no --inline=no foo.jl as suggested by @yuyichao, I get the same incorrect memory allocation. How can this result change in terms of memory if I isolate the function from the package?

Can you put this code and the function in a gist which does not load ImageQuilting at all?

Running

function grad(A)
    mx, my, mz = size(A)
    ∇xₐ = similar(A)
    for i=1:mx, j=1:my, k=1:mz
        ∇xₐ[i,j,k] = A[i,j,k]
    end
    return ∇xₐ
end

A = rand(30, 30, 1)
grad(A)

@time grad(A)

Profile.clear_malloc_data()
grad(A)

via julia --track-allocation=user --compilecache=no --inline=no foo.jl returns

  0.000707 seconds (5.37 k allocations: 137.625 KB)

and the following mem-file:

        - function grad(A)
        -     mx, my, mz = size(A)
     7888     ∇xₐ = similar(A)
        0     for i=1:mx, j=1:my, k=1:mz
   129600         ∇xₐ[i,j,k] = A[i,j,k]
        -     end
        0     return ∇xₐ
        - end

Changing the loop to

    ∇xₐ = similar(A)
    for i in indices(A) #i=1:mx, j=1:my, k=1:mz
        ∇xₐ[i] = A[i]
    end

gives

     7888     ∇xₐ = similar(A)
      304     for i in indices(A) #i=1:mx, j=1:my, k=1:mz
     1056         ∇xₐ[i] = A[i]
        -     end

though.

Thank you @pfitzseb, you are the first to reproduce what I am trying to say. The issue is pretty clear, Julia v0.5 is not allocating memory correctly and your simple example extracted from my code shows this.

@nalimilan please check this answer, as I mentioned there is no benefit in separating this function because the issue is crystal clear.

Hi

Running as you had, I get similar results

0.000585 seconds (5.55 k allocations: 141.844 KB)

However running it as

0.000020 seconds (132 allocations: 14.938 KB)

With the mem file only showing allocation on the similar function and nothing inside the loop. Thus it seems that this issue is somehow related to inline being turned on or off. Atleast for the small code snipped version, if not for the original version directly from the ImageQuilting package.

Right, as @yuyichao said this probably is no problem for real use (i.e. with inlining and precompiling on):

@yuyichao what is the best way to debug memory allocation in Julia v0.5? Is it not --compilecache=no --inline=no?