Julia:函数内部的垃圾收集与全局空间的工作方式不同

Dum*_*fus 7 garbage-collection julia

我还有一个关于Julia垃圾收集的问题.这是一个最小的例子:

function OscarTheGrouch()
    A = rand(Float32, 20000, 20000);
    A = 0;
    gc();
end
Run Code Online (Sandbox Code Playgroud)

调用OscarTheGrouch()会导致RAM使用量增加1.6GB.gc()之后调用会导致它下降1.6GB.

相反,只需在全局范围内执行函数内部的代码,即执行

A = rand(Float32, 20000, 20000);
A = 0;
gc();
Run Code Online (Sandbox Code Playgroud)

在执行之前和之后保持RAM使用不变.

我以前的RAM使用问题仅仅是因为中间结果存储为ans.但是,调用whos()后调用OscarTheGrouch()显示没有存储中间数组结果.

  • 那么,记忆在哪里?函数内的垃圾收集是否与全局范围内的行为不同(不太积极?)?

我浏览了关于Julia函数文章,但没有看到任何明显的东西.

Iai*_*ing 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
Run Code Online (Sandbox Code Playgroud)

  • 啊,我看,我现在重新创造它,这是微妙的.这适用于朱莉娅的Github问题,而不是SO问题.我不确定是谁贬低了我,但是,不是很酷. (2认同)