# How can I free the memory allocated for MPS?

+1 vote

Hi Miles,

In my program, the vector<MPS> structure is used several times in some functions. I expect the memory to be automatically freed when exiting the functions, but it is not. I have tried the "swap" trick which indeed destructs the vectors, but the total memory taken by the process still keeps growing. Making the vectors global and reusing them also does not work.

Except for sum(vector <MPS>):

auto terms = vector<MPS>(N);
for (int q = 0; q < N; q++){
terms.at(q) = psi;
SqActOnOpenMPS(momentum + q, terms.at(q), Operator, sites, N, Cargs);
SqActOnOpenMPS(q, terms.at(q), Operator, sites, N, Cargs);
}
Basis.at(0) = sum(terms, Cargs);


all the other operations are on only one or two MPS in the vector. Another strange thing is that the memory usage grows when calculating the overlap of two MPS in the same vector:

Nij[i][j] = overlap(Basis.at(i), Basis.at(j));

Hij[i][j] = overlap(Basis.at(i), H, Basis.at(j));


It takes ~ 20kB for each pair when the size of the (spinless) system is 20 and bond dimension ~ 200.

Is there any suggestion? Thank you.

Yilun

commented by (25k points)
Hi Yilun,
I think I'd need to know more to answer your questions. One question is: have you run your code in debug mode to test for any memory errors? For example I see that you are using code like Nij[i][j] without bounds checking, so you might have some out of bounds accesses happening. (Instead of using [i][j] on an array to store real numbers, I'd recommend using the Matrix class which is included with ITensor; it does bounds checking when the code is compiled in debug mode.)

Also, even though the memory usage is growing, it sounds like the amounts are very small (20kB). Is it causing a problem with running your code? I wonder if the memory actually is being freed correctly but the allocator system is just choosing to continue owning this memory, perhaps as an optimization in order to be able to more quickly allocate new memory later in the program? Of course if your system memory is filling up and you are getting crashes then that's a serious problem.

Worst case, there may be a memory leak somewhere in ITensor. If so it would be good if you could email me a minimal code that I can run which reproduces this leak. Thanks -

Miles