39

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
Mateo
  • 1,494
  • 1
  • 18
  • 27

6 Answers6

14

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().

tholy
  • 11,882
  • 1
  • 29
  • 42
12

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.

niczky12
  • 4,953
  • 1
  • 24
  • 34
  • 8
    This is interesting, but unfortunately it's not completely cleared. The value is simply set to `nothing`. For example, you cannot do `julia> const x = 3`, because then you get `ERROR: cannot declare x constant; it already has a value`. – DNF Sep 11 '17 at 19:22
  • Fair enough. I stiff found this useful to free up some memory from non constant objects. – niczky12 Sep 12 '17 at 08:30
  • It'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:) – DNF Sep 12 '17 at 08:32
  • I am getting `ERROR: UndefVarError: clear! not defined` in Julia 1.4.2 – Dima Lituiev Jul 18 '20 at 18:46
  • It's interesting that you set it to the type `Nothing` instead of the singleton instance `nothing`, do you have any particular reason for doing so? – Jonas Eschmann Jan 04 '22 at 11:20
10

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.

9

Currently, one doesn't. There is, however, an issue to track that feature:

https://github.com/JuliaLang/julia/issues/2385

StefanKarpinski
  • 32,404
  • 10
  • 86
  • 111
8

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.

Long short
  • 783
  • 1
  • 6
  • 7
1

Absolutely clear!(:x) does not work with julia 0.6.0 in notebook(IJulia)! You may choose to use x = 0 as an alternative.

spg
  • 51
  • 3