Julia: How do I delete an object in memory? A = nothing instead of A = 0

Created on 19 Oct 2017  路  2Comments  路  Source: JuliaLang/julia

Here is a quote from the documentation (https://docs.julialang.org/en/latest/manual/faq/)

Julia does not have an analog of MATLAB's clear function; once a name is defined in a Julia session (technically, in module Main), it is always present.

If memory usage is your concern, you can always replace objects with ones that consume less memory. For example, if A is a gigabyte-sized array that you no longer need, you can free the memory with A = 0. The memory will be released the next time the garbage collector runs; you can force this to happen with gc().

and see this discussion: https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541.

The proposed workaround, A = 0, has the problem that if I attempt to use the variable A at a later point, there will be no error, which can be unsafe when my intended behaviour wast to clear the variable to prevent its future use.

A better solution is to use A = nothing (as suggested by aaowens in the linked discusion), because most methods will fail with a Void and throw an error. I think the documentation should suggest A = nothing to "clear" variables instead of A = 0.

Hacktoberfest doc good first issue

All 2 comments

And what's the way to do this for functions? I'm getting

julia> f = nothing
ERROR: invalid redefinition of constant f
Stacktrace:
 [1] top-level scope at none:0

There isn't one.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

omus picture omus  路  3Comments

Keno picture Keno  路  3Comments

felixrehren picture felixrehren  路  3Comments

i-apellaniz picture i-apellaniz  路  3Comments

TotalVerb picture TotalVerb  路  3Comments