How to prevent memory leak when solving a linear system of equations using left_kernel ?
I am having a problem when running the left_kernel function multiple times. Every time I call the function It takes a new part of the memory although I do not create new variables. I tried finding out where does the memory disappear, but without any luck. here is an example code:
sage: mat
69 x 70 dense matrix over Symbolic Ring (type 'print mat.str()' to see all of the entries)
sage: get_memory_usage() #memory check before call
1170.34765625
sage: Inter_mat=mat.transpose()
sage: Solution=Inter_mat.left_kernel()
sage: get_memory_usage() #memory check after 1st call
1190.5390625
sage: Inter_mat=mat.transpose()
sage: Solution=Inter_mat.left_kernel()
sage: get_memory_usage() #memory check after 2nd call
1194.73828125
sage: Inter_mat=mat.transpose()
sage: Solution=Inter_mat.left_kernel()
sage: get_memory_usage() #memory check after 3rd call
1217.76953125
As you can see every time I call the function, the memory usage increases. Is there a way to release the memory that was used in a previous call ? My program stops after a few iterations because of lack of memory.
Update: (Creating the matrix "mat")
mat=[]
for Coord in range(len(M_col)):
if(M[M_row[Coord],M_col[Coord]]!=0):
s=M[M_row[Coord],M_col[Coord]]
if(s==1):
temp_v=vector(Poly)(x=a^M_col[Coord],y=FIELDinfoBook[M_row[Coord]])
mat.append(vector(W,temp_v))
else:
up=[i for i in range(s)]
down=list(up)
down.reverse()
for Cup in range(s):
for Cdown in range(s):
if(up[Cup]+down[Cdown]<s):
temp_v=list(zero_vector(sum(Len_Poly)))
for j in range(down[Cdown],l+1):
for i in range(up[Cup],Len_Poly[j]):
comb1=len(Combinations(j,down[Cdown]).list())
comb2=len(Combinations(i,up[Cup]).list())
temp=comb1*comb2*x^(i-up[Cup])*y^(j-down[Cdown])
temp=temp(x=a^M_col[Coord],y=FIELDinfoBook[M_row[Coord]])
temp_v[Len_Poly_inc[j]+i]=(temp)
mat.append(vector(temp_v))
The matrix M is a sparse matrix with integers (mostly ones) at certain positions. M_col and M_row are lists with the locations of nonzero elements.
sage: M
64 x 63 dense matrix over Integer Ring (type 'print M.str()' to see all of the entries)
Poly is a list of bivariate polynomials created like this:
Poly=[]
for j in range(l+1):
for i in range(Len_Poly[j]):
Poly.append(x^i*y^j)
And l=1 , Len_poly=[63, 7] and Len_poly_inc=[0, 63, 70]