Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Release memory in Julia

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?

like image 678
Thuener Avatar asked Jul 07 '16 20:07

Thuener


1 Answers

From JeffBezanson in this GitHub Issue

Yes this has to do with how code is generated. The rand call comes down to rand!(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.

like image 200
colinfang Avatar answered Oct 04 '22 18:10

colinfang