# How to solve "no more memory available" for this code?

In the codes, G is a integration depending on x and y, and tmr is defined as tmr:={x,y,G}. When I export [Table[tmr, {x, -1 * 10^(-8), 1 * 10^(-8), 1.0 * 10^(-9)}, {y, -1 * 10^(-8), 1 * 10^(-8), 1.0 * 10^(-9)}] (i.e., there is 400 calculating points), I found the memory of the computer is always increasing and will result in "no more memory available" at last. However, if I reduce the the number of the calculating points, such as [Table[tmr, {x, -1 * 10^(-8), 1 * 10^(-8), 1.0 * 10^(-8)}, {y, -1 * 10^(-8), 1 * 10^(-8), 1.0 * 10^(-8)}] ((i.e., there is 9 calculating points)), the codes can give a correct result. How to solve this problem? Many thanks!

The codes are as following:

Clear["*"]
mu = 5;
HBAR = 1.05457266*10^(-34);
ME = 9.1093897*10^(-31);
ELEC = 1.60217733*10^(-19);
kc = Sqrt[2*ME*ELEC/HBAR^2];
k = kc*Sqrt[mu];
afa = Pi/90;
d = 0.335*10^(-9);
v1 = kc^2*3;
v2 = kc^2*2;
v3 = kc^2*1;
K = 2.95*10^(10);
r = {x, y, 0};
Kh[0] = {0, 0, 0};
Ks[0] = {0, 0, 0};
Kh[1] = {K, 0, 0};
Ks[1] = {K*Cos[afa], K*Sin[afa], 0};
Kh[2] = {K/2, K*Sqrt[3]/2, 0};
Ks[2] = {K*Cos[afa + Pi/3], K*Sin[afa + Pi/3], 0};
Kh[3] = {-K/2, K*Sqrt[3]/2, 0};
Ks[3] = {K*Cos[afa + 2*Pi/3], K*Sin[afa + 2*Pi/3], 0};
Kh[4] = {-K, 0, 0};
Ks[4] = {K*Cos[afa + Pi], K*Sin[afa + Pi], 0};
Kh[5] = {-K/2, -K*Sqrt[3]/2, 0};
Ks[5] = {K*Cos[afa + 4*Pi/3], K*Sin[afa + 4*Pi/3], 0};
Kh[6] = {K/2, -K*Sqrt[3]/2, 0};
Ks[6] = {K*Cos[afa + 5*Pi/3], K*Sin[afa + 5*Pi/3], 0};
integrand[theta_?NumericQ, fi_?NumericQ] :=
Module[{ks, p, M1, M2, S1, S2, S, T, S11, S12, S13, S14, S15, S16,
S17, S21, S22, S23, S24, S25, S26, S27, S31, S32, S33, S34, S35,
S36, S37, S41, S42, S43, S44, S45, S46, S47, S51, S52, S53, S54,
S55, S56, S57, S61, S62, S63, S64, S65, S66, S67, S71, S72, S73,
S74, S75, S76, S77},
ks = {k*Sin[theta]*Cos[fi], k*Sin[theta]*Sin[fi], k*Cos[theta]};
p[i_] := k^2 - Total[(ks + Kh[i])^2];
M1 = {{0, v1, v1, v1, v1, v1, v1}, {v1, p[1], v1, v2, v3, v2,
v1}, {v1, v1, p[2], v1, v2, v3, v2}, {v1, v2, v1, p[3], v1, v2,
v3}, {v1, v3, v2, v1, p[4], v1, v2}, {v1, v2, v3, v2, v1, p[5],
v1}, {v1, v1, v2, v3, v2, v1, p[6]}};
M2 := {{0, v1*Exp[I*Total[(Ks[0] - Ks[1] - Kh[0] + Kh[1])*r]],
v1*Exp[I*Total[(Ks[0] - Ks[2] - Kh[0] + Kh[2])*r]],
v1*Exp[I*Total[(Ks[0] - Ks[3] - Kh[0] + Kh[3])*r]],
v1*Exp[I*Total[(Ks[0] - Ks[4] - Kh[0] + Kh[4])*r]],
v1*Exp[I*Total[(Ks[0] - Ks[5] - Kh[0] + Kh[5])*r]],
v1*Exp[I*Total[(Ks[0] - Ks[6] - Kh[0] + Kh[6])*r]]}, {v1*
Exp[I*Total[(Ks[1] - Ks[0] - Kh[1] + Kh[0])*r]], p[1],
v1*Exp[I*Total[(Ks[1] - Ks[2] - Kh[1] + Kh[2])*r]],
v2*Exp[I*Total[(Ks[1] - Ks[3] - Kh[1] + Kh[3])*r]],
v3*Exp[I*Total[(Ks[1] - Ks[4] - Kh[1] + Kh[4])*r]],
v2*Exp[I*Total[(Ks[1] - Ks[5] - Kh[1] + Kh[5])*r]],
v1*Exp[I*Total[(Ks[1] - Ks[6] - Kh[1] + Kh[6])*r]]}, {v1*
Exp[I*Total[(Ks[2] - Ks[0] - Kh[2] + Kh[0])*r]],
v1*Exp[I*Total[(Ks[2] - Ks[1] - Kh[2] + Kh[1])*r]], p[2],
v1*Exp[I*Total[(Ks[2] - Ks[3] - Kh[2] + Kh[3])*r]],
v2*Exp[I*Total[(Ks[2] - Ks[4] - Kh[2] + Kh[4])*r]],
v3*Exp[I*Total[(Ks[2] - Ks[5] - Kh[2] + Kh[5])*r]],
v2*Exp[I*Total[(Ks[2] - Ks[6] - Kh[2] + Kh[6])*r]]}, {v1*
Exp[I*Total[(Ks[3] - Ks[0] - Kh[3] + Kh[0])*r]],
v2*Exp[I*Total[(Ks[3] - Ks[1] - Kh[3] + Kh[1])*r]],
v1*Exp[I*Total[(Ks[3] - Ks[2] - Kh[3] + Kh[2])*r]], p[3],
v1*Exp[I*Total[(Ks[3] - Ks[4] - Kh[3] + Kh[4])*r]],
v2*Exp[I*Total[(Ks[3] - Ks[5] - Kh[3] + Kh[5])*r]],
v3*Exp[I*Total[(Ks[3] - Ks[6] - Kh[3] + Kh[6])*r]]}, {v1*
Exp[I*Total[(Ks[4] - Ks[0] - Kh[4] + Kh[0])*r]],
v3*Exp[I*Total[(Ks[4] - Ks[1] - Kh[4] + Kh[1])*r]],
v2*Exp[I*Total[(Ks[4] - Ks[2] - Kh[4] + Kh[2])*r]],
v1*Exp[I*Total[(Ks[4] - Ks[3] - Kh[4] + Kh[3])*r]], p[4],
v1*Exp[I*Total[(Ks[4] - Ks[5] - Kh[4] + Kh[5])*r]],
v2*Exp[I*Total[(Ks[4] - Ks[6] - Kh[4] + Kh[6])*r]]}, {v1*
Exp[I*Total[(Ks[5] - Ks[0] - Kh[5] + Kh[0])*r]],
v2*Exp[I*Total[(Ks[5] - Ks[1] - Kh[5] + Kh[1])*r]],
v3*Exp[I*Total[(Ks[5] - Ks[2] - Kh[5] + Kh[2])*r]],
v2*Exp[I*Total[(Ks[5] - Ks[3] - Kh[5] + Kh[3])*r]],
v1*Exp[I*Total[(Ks[5] - Ks[4] - Kh[5] + Kh[4])*r]], p[5],
v1*Exp[I*Total[(Ks[5] - Ks[6] - Kh[5] + Kh[6])*r]]}, {v1*
Exp[I*Total[(Ks[6] - Ks[0] - Kh[6] + Kh[0])*r]],
v1*Exp[I*Total[(Ks[6] - Ks[1] - Kh[6] + Kh[1])*r]],
v2*Exp[I*Total[(Ks[6] - Ks[2] - Kh[6] + Kh[2])*r]],
v3*Exp[I*Total[(Ks[6] - Ks[3] - Kh[6] + Kh[3])*r]],
v2*Exp[I*Total[(Ks[6] - Ks[4] - Kh[6] + Kh[4])*r]],
v1*Exp[I*Total[(Ks[6] - Ks[5] - Kh[6] + Kh[5])*r]], p[6]}};
S1 := Transpose[Eigenvectors[M1]].DiagonalMatrix[
Exp[I*d*(Sqrt[(k*Cos[theta])^2 + Eigenvalues[M1]] -
k*Cos[theta])]].Inverse[Transpose[Eigenvectors[M1]]];
S2 := Transpose[Eigenvectors[M2]].DiagonalMatrix[
Exp[I*d*(Sqrt[(k*Cos[theta])^2 + Eigenvalues[M2]] -
k*Cos[theta])]].Inverse[Transpose[Eigenvectors[M2]]];
S := S1.S2;
{{S11, S12, S13, S14, S15, S16, S17}, {S21, S22, S23, S24, S25,
S26, S27}, {S31, S32, S33, S34, S35, S36, S37}, {S41, S42, S43,
S44, S45, S46, S47}, {S51, S52, S53, S54, S55, S56, S57}, {S61,
S62, S63, S64, S65, S66, S67}, {S71, S72, S73, S74, S75, S76,
S77}} = S;
T = S11*Conjugate[S11] + S21*Conjugate[S21] + S31*Conjugate[S31] +
S41*Conjugate[S41] + S51*Conjugate[S51] + S61*Conjugate[S61] +
S71*Conjugate[
S71] + (Conjugate[S11]*S21*Exp[I*Total[(Kh[1] - Kh[0])*r]] +
Conjugate[S11]*S31*Exp[I*Total[(Kh[2] - Kh[0])*r]] +
Conjugate[S11]*S41*Exp[I*Total[(Kh[3] - Kh[0])*r]] +
Conjugate[S11]*S51*Exp[I*Total[(Kh[4] - Kh[0])*r]] +
Conjugate[S11]*S61*Exp[I*Total[(Kh[5] - Kh[0])*r]] +
Conjugate[S11]*S71*Exp[I*Total[(Kh[6] - Kh[0])*r]] +
Conjugate[S21]*S31*Exp[I*Total[(Kh[2] - Kh[1])*r]] +
Conjugate[S21]*S41*Exp[I*Total[(Kh[3] - Kh[1])*r]] +
Conjugate[S21]*S51*Exp[I*Total[(Kh[4] - Kh[1])*r]] +
Conjugate[S21]*S61*Exp[I*Total[(Kh[5] - Kh[1])*r]] +
Conjugate[S21]*S71*Exp[I*Total[(Kh[6] - Kh[1])*r]] +
Conjugate[S31]*S41*Exp[I*Total[(Kh[3] - Kh[2])*r]] +
Conjugate[S31]*S51*Exp[I*Total[(Kh[4] - Kh[2])*r]] +
Conjugate[S31]*S61*Exp[I*Total[(Kh[5] - Kh[2])*r]] +
Conjugate[S31]*S71*Exp[I*Total[(Kh[6] - Kh[2])*r]] +
Conjugate[S41]*S51*Exp[I*Total[(Kh[4] - Kh[3])*r]] +
Conjugate[S41]*S61*Exp[I*Total[(Kh[5] - Kh[3])*r]] +
Conjugate[S41]*S71*Exp[I*Total[(Kh[6] - Kh[3])*r]] +
Conjugate[S51]*S61*Exp[I*Total[(Kh[5] - Kh[4])*r]] +
Conjugate[S51]*S71*Exp[I*Total[(Kh[6] - Kh[4])*r]] +
Conjugate[S61]*S71*Exp[I*Total[(Kh[6] - Kh[5])*r]]) +
Conjugate[
Conjugate[S11]*S21*Exp[I*Total[(Kh[1] - Kh[0])*r]] +
Conjugate[S11]*S31*Exp[I*Total[(Kh[2] - Kh[0])*r]] +
Conjugate[S11]*S41*Exp[I*Total[(Kh[3] - Kh[0])*r]] +
Conjugate[S11]*S51*Exp[I*Total[(Kh[4] - Kh[0])*r]] +
Conjugate[S11]*S61*Exp[I*Total[(Kh[5] - Kh[0])*r]] +
Conjugate[S11]*S71*Exp[I*Total[(Kh[6] - Kh[0])*r]] +
Conjugate[S21]*S31*Exp[I*Total[(Kh[2] - Kh[1])*r]] +
Conjugate[S21]*S41*Exp[I*Total[(Kh[3] - Kh[1])*r]] +
Conjugate[S21]*S51*Exp[I*Total[(Kh[4] - Kh[1])*r]] +
Conjugate[S21]*S61*Exp[I*Total[(Kh[5] - Kh[1])*r]] +
Conjugate[S21]*S71*Exp[I*Total[(Kh[6] - Kh[1])*r]] +
Conjugate[S31]*S41*Exp[I*Total[(Kh[3] - Kh[2])*r]] +
Conjugate[S31]*S51*Exp[I*Total[(Kh[4] - Kh[2])*r]] +
Conjugate[S31]*S61*Exp[I*Total[(Kh[5] - Kh[2])*r]] +
Conjugate[S31]*S71*Exp[I*Total[(Kh[6] - Kh[2])*r]] +
Conjugate[S41]*S51*Exp[I*Total[(Kh[4] - Kh[3])*r]] +
Conjugate[S41]*S61*Exp[I*Total[(Kh[5] - Kh[3])*r]] +
Conjugate[S41]*S71*Exp[I*Total[(Kh[6] - Kh[3])*r]] +
Conjugate[S51]*S61*Exp[I*Total[(Kh[5] - Kh[4])*r]] +
Conjugate[S51]*S71*Exp[I*Total[(Kh[6] - Kh[4])*r]] +
Conjugate[S61]*S71*Exp[I*Total[(Kh[6] - Kh[5])*r]]];
Re[k^2*Sin[2*theta]*T]];
G := NIntegrate[integrand[theta, fi], {theta, 0, Pi/2}, {fi, 0, 2*Pi}];
tmr := {x, y, G};
Export["D://test.txt", Partition[Flatten[Table[tmr, {x, -1*10^(-8), 1*10^(-8), 1.0*10^(-9)}, {y, -1*10^(-8), 1*10^(-8), 1.0*10^(-9)}]], 3], "Table"];

• Which version of Mathematica are you using. There was am unavoidable memory leak in NIntegrate` in earlier versions of Mathematica (fixed around version 11 or so, I think?). If you are using an earlier version, that might be the problem, and the solution is to break the big table into smaller tables, and quit the kernel in between calculations of the different tables. Commented Oct 12, 2023 at 4:26
• Commented Oct 12, 2023 at 4:27
• Thank you very much for your answer! Commented Oct 12, 2023 at 4:42
• So, is this the problem? Are you using a pre version-10.2 version of Mathematica? Let me know, because if so, I will vote to mark this one as a duplicate. Commented Oct 12, 2023 at 16:09
• No, I am using version-13 of Mathematica, so it is not the problem of version. However, to break the big table into smaller tables is indeed a method. Commented Oct 14, 2023 at 0:59