I'm trying to release memory in julia but without success.
function memuse()
return string(round(Int,parse(Int,readall(`ps -p 29563 -o rss=`))/1024),"M")
end
function test()
for i = 1:2 println("\ni=$i")
a = rand(10000,10000)
println("Created a $(memuse())")
a = 0
gc()
println("Release a $(memuse())\n")
b = rand(10000,10000)
println("Created b $(memuse())")
b = 0
gc()
println("Release b $(memuse())\n")
c = rand(10000,10000)
println("Created c $(memuse())")
c =0
gc()
println("Release c $(memuse())\n")
end
end
Output:
i=1
Created a 918M
Release a 918M
Created b 1681M
Release b 1681M
Created c 2444M
Release c 2444M
i=2
Created a 3207M
Release a 2444M
Created b 3207M
Release b 2444M
Created c 3207M
Release c 2444M
This code only needs 918M to run but uses 3207M.
Questions: Why gc() is not releasing unused memory? There is some way to force garbage collector to release? Why garbage collector releases some memory only on the second iteration?
From JeffBezanson in this GitHub Issue
Yes this has to do with how code is generated. The
rand
call comes down torand!(Array(T, dims))
, and the inner Array remains on the "argument stack", since those slots generally get reused. It would be good to null out those slots in a case like this, but it has to be done very carefully to avoid adding lots of unnecessary stores.
You can workaround like this:
@noinline function wrap()
rand(10000,10000)
end
function test()
for i = 1:2 println("\ni=$i")
a = wrap()
println("Created a $(memuse())")
a = 0
gc()
println("Release a $(memuse())\n")
b = wrap()
println("Created b $(memuse())")
b = 0
gc()
println("Release b $(memuse())\n")
c = wrap()
println("Created c $(memuse())")
c =0
gc()
println("Release c $(memuse())\n")
end
end
Note the @noinline
so that the stack is cleared.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With