How does one clear or remove a global in julia?
Solution 1
See the latest answer to this question here: https://docs.julialang.org/en/v1/manual/faq/#How-do-I-delete-an-object-in-memory%3F
Retrieved from the docs:
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().
Solution 2
Julia 0.6 < 1.0
In Julia 0.6. You can remove the variable and free up it's memory by calling clear!()
.
You have to call clear! on the symbolic name of the variable:
julia> x = 5
5
julia> sizeof(x)
8
julia> clear!(:x)
julia> sizeof(x)
0
As DFN pointed out, this won't actually remove the objects but set them to nothing
. This is useful for freeing up memory from you workspace as you can "delete" the memory footprint for non-constant objects.
Julia 1.0+
This does not work in Julia 1.0+. If you are using 1.0+ you will have to set the object to Nothing
and let the garbage collector take it from there.
This is from the official docs here.
Solution 3
As of 0.3.9, it's possible to clear all global variables (get a new workspace), through the workspace() function.
It's also possible to get the variables from the last workspace by using LastMain (e.g. LastMain.foobar).
So currently the only way of doing what you desire, is to clear everything and transfer everything but the variable you want to your new workspace.
Solution 4
For Julia-0.6.4,
clear!(:x)
is working as mentioned by @niczky AND it's working in iJulia.
However, for Julia-1.0.0,
clear!(:x)
... throws up the following:
ERROR: UndefVarError: clear! not defined
Stacktrace:
[1] top-level scope at none:0
So, it's broken for Julia-1.0.0.
Solution 5
Currently, one doesn't. There is, however, an issue to track that feature:
Mateo
Programming Languages freak. Foodie extraordinaire. Better at ping-pong than the average Chinese.
Updated on September 18, 2020Comments
-
Mateo about 3 years
Is there any syntax that does something similar to MATLAB's "clear" i.e. if I have a global variable "a". How do I get rid of it? How do I do the analog of
clear a
-
DNF about 6 yearsThis is interesting, but unfortunately it's not completely cleared. The value is simply set to
nothing
. For example, you cannot dojulia> const x = 3
, because then you getERROR: cannot declare x constant; it already has a value
. -
niczky12 about 6 yearsFair enough. I stiff found this useful to free up some memory from non constant objects.
-
DNF about 6 yearsIt's absolutely useful, and, I guess, as close to an official solution that there is. It just has this weakness. And it was new to me:)
-
Dima Lituiev over 3 yearsI am getting
ERROR: UndefVarError: clear! not defined
in Julia 1.4.2 -
Salvatore Cosentino about 3 yearsThe above link is broken.
-
PatrickT over 2 yearsHow about
exit()
in the Atom/Juno REPL, any problems with that? Thanks. -
Jonas Eschmann almost 2 yearsIt's interesting that you set it to the type
Nothing
instead of the singleton instancenothing
, do you have any particular reason for doing so?