# Memory using in a for loop

**URL:** <https://discourse.julialang.org/t/memory-using-in-a-for-loop/3305>\
**Category:** Optimization (Mathematical)\
**Created:** [April 20, 2017, 10:48pm UTC](https://discourse.julialang.org/t/memory-using-in-a-for-loop/3305 "2017-04-20T22:48:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yankai\_Cao](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yankai\_Cao](https://discourse.julialang.org/u/Yankai_Cao)\
**Post date:** [April 20, 2017, 10:48pm UTC](https://discourse.julialang.org/t/memory-using-in-a-for-loop/3305/1 "2017-04-20T22:48:11Z")

</div>

I find that if I create and solve models in a for loop, then the memory usage will increase dramatically. I would expect the memory allocated keeps constant.

Here is the test code:

```julia
using JuMP, SCIP #,Gurobi 

function test(iter)
for i = 1:iter
    m= Model()
    nvar = 1000
    @variable(m, 0<=x[1:nvar]<=1)
    @constraint(m, con[i in 1:(nvar-2)], x[i]+x[i+1]<=1)
    @objective(m, :Min, sum{(x[i]+x[i+2])^2, i in 1:(nvar-2)})
    m.solver=SCIPSolver("display/verblevel", 0)
    #m.solver=GurobiSolver(LogToConsole=0,OutputFlag=0)
    solve(m)
    m = 1
    gc()
end
end

test(1)
println(@time test(1))
println(@time test(10))

```

When I use SCIP I got

```julia
 1.076273 seconds (96.84 k allocations: 72.203 MB, 4.01% gc time) 
 11.053048 seconds (967.77 k allocations: 721.995 MB, 4.07% gc time)

```

When I use Gurobi I got

```julia
  0.076081 seconds (90.31 k allocations: 49.162 MB, 68.42% gc time)
  0.685412 seconds (884.67 k allocations: 490.791 MB, 65.15% gc time)

```

So, the memory allocated increases as the number of iterations, which is surprising given the same problem is solve at each iteration and gc() is called explicitly at the end of each iteration.

When I monitor with top command in linux and run with `test(1000)`. I find the memory usage with `Gurobi ` keeps at 10G but the memory usage with SCIP starts with 10G but keeps at 30G.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 20, 2017, 11:10pm UTC](https://discourse.julialang.org/t/memory-using-in-a-for-loop/3305/2 "2017-04-20T23:10:00Z")

</div>

THe memory usage explicted says it’s “allocation” not peak memory usage.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 20, 2017, 11:10pm UTC](https://discourse.julialang.org/t/memory-using-in-a-for-loop/3305/3 "2017-04-20T23:10:59Z")

</div>

Also, don’t call `gc()` manually.

---

<div class="post-metadata">

**Author:** ![Yankai\_Cao](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yankai\_Cao](https://discourse.julialang.org/u/Yankai_Cao)\
**Post date:** [April 21, 2017, 1:58am UTC](https://discourse.julialang.org/t/memory-using-in-a-for-loop/3305/4 "2017-04-21T01:58:16Z")

</div>

Thanks very much for your clarification.

I modified the test a little bit and monitored the peak memory usage using /usr/bin/time -v julia test.jl

Here is the test code:  
using JuMP, Gurobi, SCIP

function test(iter)  
for i = 1:iter  
m= Model()  
nvar = 1000  
@variable(m, 0\<=x[1:nvar]\<=1)  
@constraint(m, con[i in 1:(nvar-2)], x[i]+x[i+1]\<=1)  
@objective(m, :Min, sum{(x[i]+x[i+2])^2, i in 1:(nvar-2)})  
m.solver=SCIPSolver(“display/verblevel”, 0)  
#m.solver=GurobiSolver(LogToConsole=0,OutputFlag=0)  
solve(m)  
end  
end  
test(10)

For SCIP, I got  
test(1)  
Maximum resident set size (kbytes): 234312  
test(10)  
Maximum resident set size (kbytes): 803168  
test(100)  
Maximum resident set size (kbytes): 6441800

For Gurobi, I got  
test(1)  
Maximum resident set size (kbytes): 174972  
test(10)  
Maximum resident set size (kbytes): 187388  
test(100)  
Maximum resident set size (kbytes): 263780

So for Gurobi, the result is as expected, but for SCIP the peak memory usage do increases dramatically. I will send this to SCIP community.
