Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

  • So, where is the memory being held? Does garbage collection inside functions behave differently (less aggressively?) than in the global scope?

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

like image 280
DumpsterDoofus Avatar asked Sep 16 '14 14:09

DumpsterDoofus


People also ask

Does Julia use garbage collection?

Julia's garbage collector algorithm is called mark and sweep. This algorithm consists of two phases: the mark phase, where all objects that are not garbage are found and marked so; and the sweep phase, where all unmarked objects are cleaned.


1 Answers

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
like image 102
IainDunning Avatar answered Jan 14 '23 20:01

IainDunning