# Reduce memory allocation for repeatedly solving problem using Optim.jl

**URL:** <https://discourse.julialang.org/t/reduce-memory-allocation-for-repeatedly-solving-problem-using-optim-jl/52292>\
**Category:** Optimization (Mathematical)\
**Tags:** performance, optim, memory-allocation\
**Created:** [December 23, 2020, 8:16pm UTC](https://discourse.julialang.org/t/reduce-memory-allocation-for-repeatedly-solving-problem-using-optim-jl/52292 "2020-12-23T20:16:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![MFairley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mfairley/32/8599_2.png) [@MFairley](https://discourse.julialang.org/u/MFairley)\
**Post date:** [December 23, 2020, 8:16pm UTC](https://discourse.julialang.org/t/reduce-memory-allocation-for-repeatedly-solving-problem-using-optim-jl/52292/1 "2020-12-23T20:16:29Z")

</div>

I’m trying to re-solve a problem with different parameter values many times using Optim.jl. My current code allocates a lot of memory because a new TwiceDifferentiable and TwiceDifferentiableConstraints is created for every new set of parameter values. Is there a way to cache the problem structure to prevent the memory allocation? Perhaps someone with knowledge of the internal workings of Optim.jl knows how to do this. Here’s a simple MWE:

```julia
using Random
using Optim, NLSolversBase
using BenchmarkTools

function fun(x, w)
    mapreduce((xi, wi) -> xi * wi, +, x, w)
end

function fun_grad!(g, x, w)
    g .= w
end

function fun_hess!(h, x, w)
    h .= 0.0
end

const n = 10
const x0 = zeros(n)
const lx = ones(n) * -1.0
const ux = ones(n)

function solve_subproblem(w)
    f = (x) -> fun(x, w)
    g! = (g, x) -> fun_grad!(g, x, w)
    h! = (h, x) -> fun_hess!(h, x, w)

    df = TwiceDifferentiable(f, g!, h!, x0)
    dfc = TwiceDifferentiableConstraints(lx, ux)
    
    return optimize(df, dfc, x0, IPNewton())
end

function solve(m)
    for i = 1:m
        w = rand(n)
        solve_subproblem(w)
    end
end

@benchmark solve(10000)

```

```julia
BenchmarkTools.Trial: 
  memory estimate: 9.01 GiB
  allocs estimate: 73275049
  --------------
  minimum time: 6.539 s (5.82% GC)
  median time: 6.539 s (5.82% GC)
  mean time: 6.539 s (5.82% GC)
  maximum time: 6.539 s (5.82% GC)
  --------------
  samples: 1
  evals/sample: 1

```
