Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DenseMatrix.OfRowArrays().LU() Memory Leak #1050

Open
tianmafly opened this issue Dec 8, 2023 · 1 comment
Open

DenseMatrix.OfRowArrays().LU() Memory Leak #1050

tianmafly opened this issue Dec 8, 2023 · 1 comment

Comments

@tianmafly
Copy link

hi, I test the code, and find the memory of pc is continuously growing。
Does I use not correct? thanks.

float[][] a= ...;
DenseMatrix matrix= DenseMatrix.OfRowArrays(a);

float[][] b= ...;
for (int i = 0; i < b.Length; i++)
{
DenseVector v = new DenseVector(b[i]);
// Memory Leak
LU l = matrix.LU();
Vector datas = l.Solve(v);
}

@wo80
Copy link

wo80 commented Dec 16, 2023

It seems you are factorizing the same matrix in each loop. This will allocate new memory in each iteration creating memory pressure. It's not a memory leak, the memory will be garbage collected. Still it might impact the runtime.

Creating the LU factorization before the loop should solve your problem.

There's also an overload of the Solve method to avoid allocating memory for the result inside the loop. Just allocate your datas vector before the loop and then use

/// <summary>
/// Solves a system of linear equations, <c>Ax = b</c>, with A LU factorized.
/// </summary>
/// <param name="input">The right hand side vector, <c>b</c>.</param>
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <c>x</c>.</param>
public override void Solve(Vector<float> input, Vector<float> result)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants