# 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:** 5\
**Page:** 2

<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, 3:26am UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/21 "2017-05-20T03:26:53Z")

</div>

The allocation occurs for the simplified code posted in the  
gist above even if the called function is defined as

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

```

The expansion is

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

Body:
  begin 
      return 1.0
  end::Float64

```

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [May 20, 2017, 4:10am UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/22 "2017-05-20T04:10:19Z")

</div>

Dispatch doesn’t propagate type info for keywords, so FESetT3 is typed as Any.  
Here is the problematic code:

```julia
type FESetT3 <: FESet2Manifold
  nfensperfe::FInt
  conn::FIntMat
  label::FIntVec

  function FESetT3(; conn::FIntMat=[], label =zero(FInt))
    const nfensperfe::FInt = 3
    @assert (size(conn, 2) == nfensperfe) "Number of nodes per element mismatched"
    # Needs to make a COPY of the input arrays
    self = new(nfensperfe, deepcopy(conn), deepcopy(FInt[]))
    return self
  end
end

```

This is faster:

```julia
  function FESetT3( conn::FIntMat=[], label =zero(FInt))
# etc.

```

I leave it for the experts to say whether the inference engine is working properly here. Thanks for the puzzle of the day.

---

<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:51pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/23 "2017-05-20T12:51:27Z")

</div>

Thank you very much dear Sir, this was bugging me no end.

But, how did you figure it out? Where could you see that there was a type instability? What did you do? Or was there something that bit you too?

Petr

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [May 20, 2017, 1:57pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/24 "2017-05-20T13:57:05Z")

</div>

This sort of thing has bit me before - keyword arguments are often tricky. In this case, profiling showed that the time was spent in the loop construct itself rather than the function called there, so I applied `code_warntype` to `run1()`, which showed the untyped argument `femm` in the `Jacobianvolume` call (hence the expensive logic), preceded by

```julia
      fes::Any = $(Expr(:invoke, MethodInstance for (::Core.#kw#Type)(::Array{Any,1}, ::Type{FESetModule.FESetT3}), :($(QuoteNode(Type))), :($(Expr(:invoke, MethodInstance for vector_any(::Any, ::Vararg{Any,N} where N), :(Base.vector_any), :(:conn), :(conn)))), :(Driver.FESetT3))) # line 13:
      femm::Any = (Driver.FEMMBase)(fes::Any)::Union{FEMMBaseModule.FEMMBase, FEMMBaseModule.FEMMBase{_,FEMMBaseModule.#otherdimensionunity} where _} # line 14:

```

which points to the constructor I am quoting in the edited version of my reply above.

---

<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, 2:08pm UTC](https://discourse.julialang.org/t/weird-memory-allocation/3796/25 "2017-05-20T14:08:30Z")

</div>

@Ralph_Smith:

Much obliged. Thanks!

Petr

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