I have a nested loop iteration scheme that’s taking up a lot of memory. I do understand that I should not have globals but even after I included everything in a function, the memory situation didn’t improve. It just accumulates after each iteration as if there is no garbage collection.
Here is a workable example that’s similar to my code.
I have two files. First, functions.jl:
##functions.jl
module functions
function getMatrix(A)
L = rand(A,A);
return L;
end
function loopOne(A, B)
res = 0;
for i = 1:B
res = inv(getMatrix(A));
end
return(res);
end
end
Second file main.jl:
##main.jl
include("functions.jl")
function main(C)
A = 50;
B = 30;
res = 0;
for i =1:C
mat = functions.loopOne(A,B);
res = mat .+ 1;
end
return res;
end
main(100)
When I execute julia main.jl
, the memory increases as I extend the loop in main(C)
(sometimes to more than millions of allocations and 10GiBs if I increase C
to 10,000).
Issue: I want to reduce the memory consumption. Since I’m not creating any new arrays throughout the loop, the consumption of memory should be pretty much close to constant regardless of how much I increase C
. However, that’s not the case here. The variables that I have are just L, A, B, res, mat
. My code just updates them throughout. I don’t understand why it’s creating new arrays.
What I want: I’m assigning new values to variables with the same names, as the previously assigned values are useless when the loop enters into a new iteration. Therefore, I want the memory usage to stay constant.
I know that the example looks useless but it resembles the structure that I have. Can someone please help? Thank you.