我还有一个关于Julia垃圾收集的问题。这是一个最小的例子:
function OscarTheGrouch()
A = rand(Float32, 20000, 20000);
A = 0;
gc();
end
调用OscarTheGrouch()
会导致RAM使用量增加1.6GB。之后调用gc()
会导致它下降1.6GB。
相反,只需在全局范围内执行函数内部的代码,即执行
A = rand(Float32, 20000, 20000);
A = 0;
gc();
在执行之前和之后保持RAM使用不变。
My previous RAM use question原因只是因为中间结果存储为ans
。但是,在调用whos()
后调用OscarTheGrouch()
表示没有存储中间数组结果。
我浏览了article on Julia functions,但没有看到任何明显的事情。
答案 0 :(得分:6)
我可以重现你的行为
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "help()" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.3.0 (2014-08-20 20:43 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-apple-darwin13.3.0
julia> # Memory now about 64M
julia> A = rand(Float32, 20000, 20000);
julia> # Memory now about 1600M
julia> A = 0
0
julia> gc()
julia> # Memory now about 75M
julia> function test1()
A = rand(Float32, 20000, 20000)
nothing
end
test1 (generic function with 1 method)
julia> test1() # Still about 78, although briefly higher
julia> function test2()
A = rand(Float32, 20000, 20000)
A = 0
end
test2 (generic function with 1 method)
julia> test2() # Same behaviour
0
julia> function test3()
A = rand(Float32, 20000, 20000)
A = 0
gc()
end
test3 (generic function with 1 method)
julia> test3() # Now at 1600M