# Clear variables in caller workspace

**URL:** <https://discourse.julialang.org/t/clear-variables-in-caller-workspace/89825>\
**Category:** Performance\
**Tags:** question, memory, functions\
**Created:** [November 5, 2022, 9:39pm UTC](https://discourse.julialang.org/t/clear-variables-in-caller-workspace/89825 "2022-11-05T21:39:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hclin](https://avatars.discourse-cdn.com/v4/letter/h/9f8e36/32.png) [@hclin](https://discourse.julialang.org/u/hclin)\
**Post date:** [November 5, 2022, 9:39pm UTC](https://discourse.julialang.org/t/clear-variables-in-caller-workspace/89825/1 "2022-11-05T21:39:29Z")

</div>

Hi all,

I want to utilize memory usage in Julia. Before I do some heavy computations, I want to clear some big matrices to optimize the memory usage in my code.

For example, I have two big matrices, `A` and `B`. In the function `combine_A_B_and_compute`, I input `A`, and `B`, and combine them to matrix `K` do some computations. Since `A` and `B` are big matrices, after building matrix `K`, I want to clear `A` and `B` (set them to be `nothing`) inside the function and in the caller workspace.

In Matlab, I can do so by `clear A B; evalin(‘caller’, [‘clear A B’])`;

In Julia, if I use

```julia
function combine_A_B_and_compute(A, B)
K = cat(A,B,dims=1)
A = nothing;
B = nothing;
GC.gc()
……
end

```

It does not clear `A` and `B` in the caller workspace.

The way I have is to define a `mutual structure Matrices` and making `A` and `B` be its fields:

```julia
mutable struct Matrices
A::Union{Matrix, Nothing}
B::Union{Matrix, Nothing}
Matrices()=new()
end

```

Then by setting the fields in `mutual struct Matrices` to nothing, I can clear `A` and `B` in the caller workspace.

```julia
function combine_A_B_and_calculate!(X::Matrices)
K = cat(X.A,X.B,dims=1)
X.A = nothing;
X.B = nothing;
GC.gc()
……
end

```

However, this way is not very intuitive.

Is there a better way to clear variables in the caller workspace in Julia? Thanks!

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 5, 2022, 11:51pm UTC](https://discourse.julialang.org/t/clear-variables-in-caller-workspace/89825/2 "2022-11-05T23:51:04Z")

</div>

There is no way to clear variables in the caller workspace. Such concept does not exist. When you call a function all your variables are either global or from your local context (i.e., the function), what you did is to create a mutable object keeping the only references to other objects in the memory, and overwriting the references inside the function, effectively losing the only reference which you did have from them, and allowing them to be garbage collected.

What you can do is:

1. What you already did.
2. Do not keep any references on the caller, instead create the objects at the function call (i.e., `f(Matrix(...), Matrix(...))`) so the code does not keep any references in the caller scope.

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [November 6, 2022, 11:22am UTC](https://discourse.julialang.org/t/clear-variables-in-caller-workspace/89825/3 "2022-11-06T11:22:15Z")

</div>

To add another option, it is typically a good idea to [put performance critical code inside a function](https://docs.julialang.org/en/v1/manual/performance-tips/#Performance-critical-code-should-be-inside-a-function). In this case, you can write

```julia
function combine_A_B_and_compute(a, b)
    k = cat(a, b, dims=1)
    compute(k)
end

function main()
    a = rand(10_000, 10_000)
    b = rand(10_000, 10_000)
    combine_A_B_and_compute(a, b)
end

main()

```

which will allow the garbage collector to automatically free the memory used by `a` and `b` as soon as control flow passes to `compute`. This achieves the same result as @Henrique_Becker’s option 2.
