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
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
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().
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.
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.
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.
Currently, one doesn't. There is, however, an issue to track that feature:
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.
Absolutely clear!(:x) does not work with julia 0.6.0 in notebook(IJulia)! You may choose to use x = 0 as an alternative.