# JuMP memory allocation

**URL:** https://discourse.julialang.org/t/jump-memory-allocation/126731
**Category:** Optimization (Mathematical)
**Tags:** jump, optimization
**Created:** [March 9, 2025, 4:26pm UTC](https://discourse.julialang.org/t/jump-memory-allocation/126731 "2025-03-09T16:26:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![fdekerme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fdekerme/32/43574_2.png) [@fdekerme](https://discourse.julialang.org/u/fdekerme)
#### Post date: [March 9, 2025, 4:26pm UTC](https://discourse.julialang.org/t/jump-memory-allocation/126731/1 "2025-03-09T16:26:52Z")

</div>

Hi! 😀  
I am starting to use JuMP.jl to implement an optimization process on an image (size 266 x 353 pixels). I implement the problem like that to find L₁, L₂ values on each pixel:

```julia
model = Model(Ipopt.Optimizer)
set_silent(model)

@variable(model, 0 <= L₁[i = 1:x, j = 1:y], start = start_values.mat1[i, j])
@variable(model, 0 <= L₂[i = 1:x, j = 1:y], start = start_values.mat2[i, j])

function f(spectra, L₁, L₂)
    sum((detector_response(spectra.top[i,j, :], L₁[i,j], L₂[i,j]) - top_image[i, j])^2 +
    (detector_response(spectra.bottom[i,j,:], L₁[i,j], L₂[i,j]) - bottom_image[i, j])^2
     for i in 1:x, j in 1:y)
end 

@objective(model, Min, f(spectra, L₁, L₂))
optimize!(model)

```

However, this process takes up an enormous amount of RAM (more than 50GB) at the `@objective(model, Min, f(spectra, L₁, L₂))` stage. What am I doing wrong and could I improve to reduce RAM usage?

Thanks,  
fdekerm

---

<div class="post-metadata">

### Author: ![BdeKoning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdekoning/32/214557_2.png) [@BdeKoning](https://discourse.julialang.org/u/BdeKoning)
#### Post date: [March 9, 2025, 4:30pm UTC](https://discourse.julialang.org/t/jump-memory-allocation/126731/2 "2025-03-09T16:30:05Z")

</div>

Slicing allocates by default in Julia. Try views: `view(spectra.top, i, j, :)`

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [March 9, 2025, 11:15pm UTC](https://discourse.julialang.org/t/jump-memory-allocation/126731/3 "2025-03-09T23:15:51Z")

</div>

Hi @fdekerme, I’ve moved this to the “Optimization (Mathematical)” section.

Can you provide a reproducible example?

The answer depends one what `detector_response` is doing. But a simple fix might be:

```Julia
using JuMP, Ipopt
model = Model(Ipopt.Optimizer)
set_silent(model)
@variable(model, 0 <= L₁[i = 1:x, j = 1:y], start = start_values.mat1[i, j])
@variable(model, 0 <= L₂[i = 1:x, j = 1:y], start = start_values.mat2[i, j])
@objective(
    model,
    Min,
    sum(
        (detector_response(spectra.top[i,j, :], L₁[i,j], L₂[i,j]) - top_image[i, j])^2 +
        (detector_response(spectra.bottom[i,j,:], L₁[i,j], L₂[i,j]) - bottom_image[i, j])^2
        for i in 1:x, j in 1:y
    )
)
optimize!(model)

```

See [Performance tips · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/performance_tips/#Use-macros-to-build-expressions)
