# Weird memory allocation

**URL:** <https://discourse.julialang.org/t/weird-memory-allocation/3796>\
**Category:** General Usage\
**Created:** [May 18, 2017, 11:41pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796 "2017-05-18T23:41:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 18, 2017, 11:41pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/1 "2017-05-18T23:41:29Z")

</div>

Every single call of this function results in memory allocation. I have narrowed it down to the call  
to `Jacobiansurface`: if that call is eliminated, there is no memory allocation.

```julia
function Jacobianvolume{T<:FESet2Manifold}(self::FEMMBase{T}, J::FFltMat,
            loc::FFltMat, conn::FIntMat, N::FFltMat)::FFlt
  Jac = Jacobiansurface(self, J, loc, conn, N)::FFlt
end

```

The function that triggers the memory allocation is this one: As you can see, trivial interior and I don’t see why any memory should be allocated since the same arguments are getting passed in.

```julia
function Jacobiansurface{T<:FESet2Manifold}(self::FEMMBase{T}, J::FFltMat,
            loc::FFltMat, conn::FIntMat, N::FFltMat)::FFlt
return 1.0
end

```

I would really appreciate some pointers. I don’t see that deep into how Julia handles these minutiae.  
The typealias `FFlt` can simply be read as `Float64`.

Thanks very much.

Petr

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 18, 2017, 11:51pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/2 "2017-05-18T23:51:24Z")

</div>

So `Jacobiansurface(self, J, loc, conn, N)` is a function that allocates and that’s it?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 18, 2017, 11:53pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/3 "2017-05-18T23:53:14Z")

</div>

If Jacobiansurface() does not get called, no allocation; if it does, there is an allocation.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 19, 2017, 1:35am UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/4 "2017-05-19T01:35:16Z")

</div>

> [@PetrKryslUCSD](#):
>
> If Jacobiansurface() does not get called, no allocation; if it does, there is an allocation.

So agrees with what I said?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 19, 2017, 1:38am UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/5 "2017-05-19T01:38:17Z")

</div>

Sorry, I don’t know if it is the function itself that allocates the memory, or if it is the caller that allocates the memory. But, as I said, if the function doesn’t get called, there is no memory allocated.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 19, 2017, 1:51am UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/6 "2017-05-19T01:51:11Z")

</div>

I’m not sure what you are asking. If you don’t see allocation if you don’t do something and see allocation if you do something, doesn’t that suggest the thing you do is causing the allocation? What’s wierd about it?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 19, 2017, 2:17am UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/7 "2017-05-19T02:17:10Z")

</div>

The weird thing is that there IS allocation happening, wouldn’t you agree? Why should a function which simply returns a constant allocate memory? Or, why should the caller allocate memory to call this function? Doesn’t this seem strange to you?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 19, 2017, 3:02am UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/8 "2017-05-19T03:02:33Z")

</div>

1. you didn’t describe how you measured this
2. if you use track-allocation then no it’s not very reliable and can (and in principle is expected to) show allocation from callee.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 19, 2017, 3:24am UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/9 "2017-05-19T03:24:36Z")

</div>

Measurement: call `Jacobianvolume` 2 million times, with and without the call to `Jacobiansurface`. Used the `@time` macro. With the call there are 2 million more allocations reported than without. Hence, I conclude that calling the `Jacobiansurface` function causes allocation to occur. I don’t know whether the allocation is in the caller or in the callee.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 19, 2017, 9:51am UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/10 "2017-05-19T09:51:21Z")

</div>

Please post the exact code you used, rather than describing it in words, since this can affect significantly the result (eg if you are timing in global scope).

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 19, 2017, 3:56pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/11 "2017-05-19T15:56:01Z")

</div>

@dpsanders:

David,

In principle that is a great suggestion, but that means distilling the code to the absolute minimum. While I’m working on that, do you think anything could be read from the following?

Running

```julia
@time for j = 1:10000000
  Jacobianvolume(femm, J, loc, conn, N) 
end
@code_warntype Jacobianvolume(femm, J, loc, conn, N) 

```

produces the timing info  
0.297144 seconds (10.00 M allocations: 152.588 MB, 3.84% gc time)

and the listing

```julia
Variables:
  #self#::FinEtools.FEMMBaseModule.#Jacobianvolume
  self::FinEtools.FEMMBaseModule.FEMMBase{FinEtools.FESetModule.FESetT3,FinEtools.FEMMBaseModule.#otherdimensionunity}
  J::Array{Float64,2}
  loc::Array{Float64,2}
  conn::Array{Int64,2}
  N::Array{Float64,2}
  Jac::Float64

Body:
  begin 
      SSAValue(0) = FinEtools.FEMMBaseModule.FFlt
      SSAValue(1) = $(Expr(:invoke, LambdaInfo for Jacobiansurface(::FinEtools.FEMMBaseModule.FEMMBase{FinEtools.FESetModule.FESetT3,FinEtools.FEMMBaseModule.#otherdimensionunity}, ::Array{Float64,2}, ::Array{Float64,2}, ::Array{Int64,2}, ::Array{Float64,2}), :(FinEtools.FEMMBaseModule.Jacobiansurface), :(self), :(J), :(loc), :(conn), :(N)))
      Jac::Float64 = SSAValue(1)
      SSAValue(2) = SSAValue(1)
      return SSAValue(2)
  end::Float64

```

Also,

```julia
function Jacobianvolume{T<:FESet2Manifold}(self::FEMMBase{T}, J::FFltMat,
            loc::FFltMat, conn::FIntMat, N::FFltMat)::FFlt
  @code_warntype Jacobiansurface(self, J, loc, conn, N)
  Jac = Jacobiansurface(self, J, loc, conn, N)::FFlt
end

```

gives

```julia
Variables:
  #self#::FinEtools.FEMMBaseModule.#Jacobiansurface
  self::FinEtools.FEMMBaseModule.FEMMBase{FinEtools.FESetModule.FESetT3,FinEtools.FEMMBaseModule.#otherdimensionunity}
  J::Array{Float64,2}
  loc::Array{Float64,2}
  conn::Array{Int64,2}
  N::Array{Float64,2}
  Jac::Float64

Body:
  begin 
      $(Expr(:inbounds, false))
      # meta: location C:\Users\Petr Krysl\Dropbox\Julia\FinEtools\src\FESetModule.jl Jacobian 227
      
SSAValue(0) = FinEtools.FESetModule.FFlt
      $(Expr(:inbounds, true))
      Jac::Float64 = (Base.box)(Base.Float64,(Base.sub_float)((Base.box)(Base.Float64,(Base.mul_float)((Base.arrayref)(J::Array{Float64,2},1,1)::Float64,(Base.arrayref)(J::Array{Float64,2},2,2)::Float64)),(Base.box)(Base.Float64,(Base.mul_float)((Base.arrayref)(J::Array{Float64,2},2,1)::Float64,(Base.arrayref)(J::Array{Float64,2},1,2)::Float64))))
      $(Expr(:inbounds, :pop))
      # meta: pop location
      $(Expr(:inbounds, :pop))
      return Jac::Float64
  end::Float64

```

I can see the `Base.box`, and I understand that can mean memory allocation. What I don’t get is why: the expression that gets boxed is `@inbounds Jac = (J[1, 1]*J[2, 2] - J[2, 1]*J[1, 2])`. Clearly the compiler sees that `J` is a double array, so why the boxing?

Thanks a lot.

Petr

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 19, 2017, 4:29pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/12 "2017-05-19T16:29:01Z")

</div>

Yes, you are timing in global scope.  
Use the BenchmarkTooks.jl package instead.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 19, 2017, 6:07pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/13 "2017-05-19T18:07:03Z")

</div>

No, the timed loop was inside a module, no global variables.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 19, 2017, 6:22pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/14 "2017-05-19T18:22:36Z")

</div>

That’s exactly what global scope means

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 19, 2017, 6:43pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/15 "2017-05-19T18:43:18Z")

</div>

Right, sorry. What I meant was the loop is inside a function in a module.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 19, 2017, 6:54pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/16 "2017-05-19T18:54:09Z")

</div>

In that case you should still post the actual full code.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 19, 2017, 7:39pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/17 "2017-05-19T19:39:16Z")

</div>

I tried to create a minimal code to reproduce the conditions, but I failed. The code shown here does not show any allocations:  
[https://gist.github.com/PetrKryslUCSD/292263d346a1c5653c07cf0dda70b7b2](https://gist.github.com/PetrKryslUCSD/292263d346a1c5653c07cf0dda70b7b2)

Nevertheless, there is something interesting here: I get the following for the function called inside the loop from this minimal code (with the line `@code_warntype FEMMBaseModule.Jacobianvolume(femm, J, loc, conn, N)`):

```julia
Variables:
  #self#::FEMMBaseModule.#Jacobianvolume
  self::FEMMBaseModule.FEMMBase{FEMMBaseModule.FESetT3,FEMMBaseModule.#otherdimensionunity}
  J::Array{Float64,2}
  loc::Array{Float64,2}
  conn::Array{Int64,2}
  N::Array{Float64,2}
  Jac::Float64

Body:
  begin 
      SSAValue(0) = FEMMBaseModule.FFlt
      SSAValue(1) = 1.0
      Jac::Float64 = SSAValue(1)
      SSAValue(2) = SSAValue(1)
      return SSAValue(2)
  end::Float64

```

From the code that reports allocation I get (to my eyes identical):

```julia
Variables:
  #self#::FinEtools.FEMMBaseModule.#Jacobianvolume
  self::FinEtools.FEMMBaseModule.FEMMBase{FinEtools.FESetModule.FESetT3,FinEtools.FEMMBaseModule.#otherdimensionunity}
  J::Array{Float64,2}
  loc::Array{Float64,2}
  conn::Array{Int64,2}
  N::Array{Float64,2}
  Jac::Float64

Body:
  begin 
      
SSAValue(0) = FinEtools.FEMMBaseModule.FFlt
      SSAValue(1) = 1.0
      Jac::Float64 = SSAValue(1)
      SSAValue(2) = SSAValue(1)
      return SSAValue(2)
  end::Float64

```

Yet, in the actual code (not the distilled fragment) each call costs an allocation.  
Any ideas please?

Thanks,

Petr

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 19, 2017, 7:55pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/18 "2017-05-19T19:55:54Z")

</div>

> [@PetrKryslUCSD](#):
>
> The code shown here does not show any allocations:

It seems the most likely that you are not measuring what you think you are. If you don’t post the actual code where you see the problem it’ll be impossible to tell what you are getting wrong.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 19, 2017, 8:01pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/19 "2017-05-19T20:01:07Z")

</div>

I would post it, but it is several dozen files. I tried to reduce it, but the resulting minimal code does not reproduce the “problem”. But what do you think of the two codes of the called function being “identical”, yet one results in memory allocations, one does not?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 20, 2017, 12:55am UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/20 "2017-05-20T00:55:50Z")

</div>

@dpsanders, @yuyichao

Here is the code reduced as much as I could. It reproduces the allocation of memory  
for each call within the loop.  
[https://gist.github.com/PetrKryslUCSD/c4c3a348dcef49690ec12bbc5543cc4b](https://gist.github.com/PetrKryslUCSD/c4c3a348dcef49690ec12bbc5543cc4b)

Petr

[Next page](https://discourse.julialang.org/t/weird-memory-allocation/3796.md?page=2)
