2

I am running an empty double loop in Julia

Ngal = 16000000
function get_vinz()
    for i in 1:5
        print(i, " ")
        for j in i:Ngal
        end
    end
end

and the outcome of @time vinz() gives me

1 2 3 4 5   5.332660 seconds (248.94 M allocations: 4.946 GiB, 7.12% gc time)

What is the 5GB of memory allocated for?

1 Answer 1

6

the culprit is the use of global variables. your function calls the global variable, and with each call, a Int64 is allocated (64 bits). 64*16000000*5/1024/1024 = 4882.8125 MiB, that seems like the culprit your function doesn't know the size of the inner loop, and does a lookup on the global scope to check Ngal. It does that every single loop. compare that with this implementation:

function get_vinz(Ngal)
    for i in 1:5
        print(i, " ")
        for j in i:Ngal
        end
    end
end
julia> @time get_vinz(Ngal)
1 2 3 4 5   0.043481 seconds (53.67 k allocations: 2.776 MiB)

also, the first time a function is called in julia, is compiled to machine code, so the subsecuent runs are fast. measuring time again:

julia> @time get_vinz(Ngal)
1 2 3 4 5   0.000639 seconds (50 allocations: 1.578 KiB)

The use of global variables is a bad practice in general. the recommended way is to pass those values to the function

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.