# Incorrect memory allocation in Julia v0.5

**URL:** <https://discourse.julialang.org/t/incorrect-memory-allocation-in-julia-v0-5/736>\
**Category:** General Usage\
**Created:** [December 5, 2016, 5:28am UTC](https://discourse.julialang.org/t/incorrect-memory-allocation-in-julia-v0-5/736 "2016-12-05T05:28:50Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [December 6, 2016, 9:32pm UTC](https://discourse.julialang.org/t/incorrect-memory-allocation-in-julia-v0-5/736/21 "2016-12-06T21:32:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 7, 2016, 7:26am UTC](https://discourse.julialang.org/t/incorrect-memory-allocation-in-julia-v0-5/736/22 "2016-12-07T07:26:17Z")

</div>

@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.

```julia
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?

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [December 7, 2016, 9:06am UTC](https://discourse.julialang.org/t/incorrect-memory-allocation-in-julia-v0-5/736/23 "2016-12-07T09:06:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [December 7, 2016, 12:30pm UTC](https://discourse.julialang.org/t/incorrect-memory-allocation-in-julia-v0-5/736/24 "2016-12-07T12:30:15Z")

</div>

Running

```julia
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

```julia
  0.000707 seconds (5.37 k allocations: 137.625 KB)

```

and the following mem-file:

```julia
        - 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

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

```

gives

```julia
     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.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 8, 2016, 3:54am UTC](https://discourse.julialang.org/t/incorrect-memory-allocation-in-julia-v0-5/736/25 "2016-12-08T03:54:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lwabeke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwabeke/32/4005_2.png) [@lwabeke](https://discourse.julialang.org/u/lwabeke)\
**Post date:** [December 8, 2016, 11:21am UTC](https://discourse.julialang.org/t/incorrect-memory-allocation-in-julia-v0-5/736/26 "2016-12-08T11:21:08Z")

</div>

Hi

Running as you had, I get similar results

> [@pfitzseb](#):
>
> julia --track-allocation=user --compilecache=no --inline=no foo.jl

0.000585 seconds (5.55 k allocations: 141.844 KB)

However running it as

> [@pfitzseb](#):
>
> julia --track-allocation=user --compilecache=no --inline=yes foo.jl

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.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [December 8, 2016, 11:28am UTC](https://discourse.julialang.org/t/incorrect-memory-allocation-in-julia-v0-5/736/27 "2016-12-08T11:28:28Z")

</div>

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

> [@yuyichao](#):
>
> There’s nothing wrong with the allocation afaict. The options to generate coverage and allocation data do not work with inlining on before 0.6.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 8, 2016, 4:45pm UTC](https://discourse.julialang.org/t/incorrect-memory-allocation-in-julia-v0-5/736/28 "2016-12-08T16:45:48Z")

</div>

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

[Previous page](https://discourse.julialang.org/t/incorrect-memory-allocation-in-julia-v0-5/736.md?page=1)
