# Understanding allocations

**URL:** <https://discourse.julialang.org/t/understanding-allocations/15967>\
**Category:** General Usage\
**Created:** [October 6, 2018, 4:25pm UTC](https://discourse.julialang.org/t/understanding-allocations/15967 "2018-10-06T16:25:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [October 6, 2018, 4:25pm UTC](https://discourse.julialang.org/t/understanding-allocations/15967/1 "2018-10-06T16:25:29Z")

</div>

Hi,

I have troubles understanding why `foo` allocates whereas `foo` does not:

```julia
 1.623 ms (65804 allocations: 1.18 MiB)
  7.578 μs (0 allocations: 0 bytes)

```

Does someone know why?

Thank you,

```julia
using Parameters, BenchmarkTools

@with_kw mutable struct Mesh
	V = randn(100,110)
end

function foo1(V,g)
	Nw,Nv = size(V)
	for j=1:Nw
		for i=1:Nv
		gij = g[j,i]
		gimj = (i > 1 ) ? g[j,i-1] : zero(gij)

		Fij = (i > 1) ? ((V[j,i]>=0.) ? V[j,i] * gimj : V[j,i] * gij) : zero(gij)

		end
	end
end

function foo(m,g)
	Nw,Nv = size(m.V)
	for j=1:Nw
		for i=1:Nv
		gij = g[j,i]
		gimj = (i > 1 ) ? g[j,i-1] : zero(gij)

		Fij = (i > 1) ? ((m.V[j,i]>=0.) ? m.V[j,i] * gimj : m.V[j,i] * gij) : zero(gij)
		end
	end
end

V0 = rand(100,110)
g1 = randn(100,110)

m = Mesh()
@btime foo($m,$g1)
@btime foo1($(m.V),$g1)

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 6, 2018, 4:27pm UTC](https://discourse.julialang.org/t/understanding-allocations/15967/2 "2018-10-06T16:27:30Z")

</div>

Your Mesh isn’t strictly typed. Do:

```julia
@with_kw mutable struct Mesh
	V::Matrix{Float64} = randn(100,110)
end

```

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [October 6, 2018, 5:20pm UTC](https://discourse.julialang.org/t/understanding-allocations/15967/3 "2018-10-06T17:20:48Z")

</div>

Well… thank you!
