I have a `for`

loop in which I create a function that I optimize, something like:

```
using Interpolations, Optim
function PolicyFunction()
V = Array{Float64}(undef, 201, 40)
n = Array{Float64}(undef, 201, 40)
grid_h = range(10.0, stop = 300, length = 201)
for j = 40:-1:1
if j == 40 # Last period
nextV = zeros(201)
else
nextV = V[:,j+1]
end
# Linerar interpolation for this period
MyItp = interpolate((grid_h,), nextV, Gridded(Linear()))
V_tw = extrapolate(MyItp, Line())
for (ind_h, h) in enumerate(grid_h)
# Define aux function to maximise
aux_f(n_td) = log(1-n_td) + (0.95*V_tw(n_td^2.0))
result = optimize(aux_f, 0.01, 0.99, GoldenSection())
Optim.converged(result) || error("Failed to converge")
n[ind_h, j] = Optim.minimizer(result)
V[ind_h, j] = Optim.minimum(result)
end
end
return n
end
```

With this implementation I get:

```
@time PolicyFunction()
0.106507 seconds (1.68 M allocations: 32.176 MiB, 3.05% gc time)
```

I’m happy with the time but I was surprised with the memory allocation (in my real application I get something like `148.13 M allocations: 2.238 GiB, 2.08% gc time`

. When I use a more rudimentary solution like:

```
function PolicyFunction()
V = Array{Float64}(undef, 201, 40)
n = Array{Float64}(undef, 201, 40)
grid_h = range(10.0, stop = 300, length = 201)
for j = 40:-1:1
# Define vector for next period
if j == 40 # Last period
nextV = zeros(201)
else
nextV = V[:,j+1]
end
# Linerar interpolation for this period
MyItp = interpolate((grid_h,), nextV, Gridded(Linear()))
V_tw = extrapolate(MyItp, Line())
for (ind_h, h) in enumerate(grid_h)
V_max = - Inf
for n_td in range(0.01, stop = 0.99, length = 201)
V_td = log(1-n_td) + (0.95*V_tw(n_td^2.0))
if V_td > V_max
V_max = V_td
n[ind_h, j] = n_td
V[ind_h, j] = V_td
end
end
end
end
return n
end
```

The time increases (of course depends on the grid points for `n_td`

) but the use of memory is much lower (in my real application the difference is even more dramatic):

```
0.195065 seconds (150.49 k allocations: 7.371 MiB, 1.22% gc time)
```

Can anybody explain why is there such a difference in memory allocation? Is it because I create a function in each iteration? Is there a way around it?