How to force an object to be freed by the garbage collector

Joris_Pinkse
Wed, 16 Mar 2022 16:56:24 +0000
General Usage

From this very old post (https://groups.google.com/g/julia-users/c/QkChqeKnd9g/m/cqltIs5doQoJ) I was led to believe that the call to gc below would free the memory allocated by X, but it doesn’t.

How could I force the release of the memory allocated by X here?

(dimensions chosen to bomb if the memory allocated by X does not get freed)

function testme()
    X = rand( 14_000_000_000 )
    Y = sum( X )
    X = nothing
    GC.gc()             # why does the memory not get freed here?
    Z = rand( 14_000_000_000 )
    Y += sum( Z )
    return Y
end

function tester()
    Y = testme()
    return Y
end

println( tester() )

20 posts - 7 participants