DumpsterDoofus
DumpsterDoofus

Reputation: 1152

Julia: garbage collection inside functions works differently than in global space

I have yet another question about garbage collection in Julia. Here is a minimal example:

function OscarTheGrouch()
    A = rand(Float32, 20000, 20000);
    A = 0;
    gc();
end

Calling OscarTheGrouch() causes RAM use to increase by 1.6GB. Calling gc() afterwards causes it to drop by 1.6GB.

In contrast, simply executing the code inside the function in the global scope, ie, executing

A = rand(Float32, 20000, 20000);
A = 0;
gc();

leaves RAM use unchanged before and after execution.

My previous RAM use question turned out to simply be due to the fact that intermediate results are stored as ans. However, calling whos() after calling OscarTheGrouch() shows no intermediate array results being stored.

I skimmed the article on Julia functions, but didn't see anything obvious.

Upvotes: 8

Views: 2402

Answers (1)

IainDunning
IainDunning

Reputation: 11664

I can reproduce your behaviour

               _
   _       _ _(_)_     |  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

Upvotes: 7

Related Questions