# Large amount of memory allocation when using autodiff and IPNewton

**URL:** <https://discourse.julialang.org/t/large-amount-of-memory-allocation-when-using-autodiff-and-ipnewton/109451>\
**Category:** Performance\
**Created:** [January 30, 2024, 1:39pm UTC](https://discourse.julialang.org/t/large-amount-of-memory-allocation-when-using-autodiff-and-ipnewton/109451 "2024-01-30T13:39:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimOb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timob/32/206645_2.png) [@TimOb](https://discourse.julialang.org/u/TimOb)\
**Post date:** [January 30, 2024, 1:39pm UTC](https://discourse.julialang.org/t/large-amount-of-memory-allocation-when-using-autodiff-and-ipnewton/109451/1 "2024-01-30T13:39:15Z")

</div>

Hi all,

I’m new to Julia and have been working on a model which essentially is a long loop over optimisation problems:

```julia
for state = 1:n

            x0 = ones(5)/2;
            fun1(x) = -hh_utility(x, state)[1];
            cfg = ForwardDiff.GradientConfig(fun1, x0, ForwardDiff.Chunk{5}());
            cfg2 = ForwardDiff.HessianConfig(fun1, x0, ForwardDiff.Chunk{5}());
            grad1(g, x) = ForwardDiff.gradient!(g, fun1, x, cfg);
            hes1(H, x) = ForwardDiff.hessian!(H, fun1, x, cfg2);
            df = TwiceDifferentiable(fun1, grad1, hes1, x0);
            res = optimize(df, dfc, x0, IPNewton());
            output[state, :] = res.minimizer;

end

```

When I run the code, I get the following timing:

127.602522 seconds (2.09 G allocations: 161.172 GiB, 18.27% gc time, 0.42% compilation time)

The memory allocation seems quite large and I was wondering whether I should try to reduce it or whether it’s normal? If I just check the function and derivatives, I get these allocations:

```julia
@time fun1(x0)
  0.000014 seconds (3 allocations: 80 bytes)

@time grad1(g, x0)
  0.000034 seconds (17 allocations: 1.297 KiB)

@time hes1(H, x0)
  0.000058 seconds (21 allocations: 9.703 KiB)

```

In case I should try to reduce allocations, what could be a good way of doing that?

Tim

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [January 30, 2024, 4:02pm UTC](https://discourse.julialang.org/t/large-amount-of-memory-allocation-when-using-autodiff-and-ipnewton/109451/2 "2024-01-30T16:02:37Z")

</div>

There could be potential for optimisation.

In general, I like this reference: [https://modernjuliaworkflows.github.io/](https://modernjuliaworkflows.github.io/)

For your case, you could also use the profiler [Profiler · Julia in VS Code](https://www.julia-vscode.org/docs/stable/userguide/profiler/) to find out where the allocations are coming from.

About your code: You might want to initialise the gradient configs outside of the for loop. In addition, there might be some type instability in ‘hh\_utility’ which would then make everything less efficient. Is it possible to share ‘hh\_utility’?
