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
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:
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.
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!