<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><br class=""></div>  I assume you are calling KSPSetOperators() before each new KSPSolve() so that it knows to solve a new system?<div class=""><br class=""></div><div class="">  If the above is true with your current code it should thus have two matrices in memory at the most since every new call to KSPSetOperators() will trigger freeing the previous.</div><div class=""><br class=""></div><div class=""> In C you could do </div><div class=""><br class=""></div><div class="">   KSPSetOperators(ksp,NULL,NULL)</div><div class=""><br class=""></div><div class="">immediately after the KSPSolve() (at the same time that you destroy the matrix) this will cause KSP to decrease the reference count on the matrix so it will be freed and you will only ever have one matrix in memory at a time.</div><div class=""><br class=""></div><div class="">In fortran </div><div class=""><br class=""></div><div class="">KSPSetOperators(ksp,PETSC_NULL_MAT,PETSC_NULL_MAT)</div><div class=""><br class=""></div><div class="">might work.</div><div class=""><br class=""></div><div class="">Depending on your individual systems and the preconditioners you are using there might be a good amount of reuse of preconditioner information from each solve that could decrease the run time. This would not happen if you reuse the KSP for multiple different linear systems, then each solve requires a complete rebuild of the preconditioner.  So you may be trading speed for memory usage and might consider using a computing system with more memory.</div><div class=""><br class=""></div><div class="">Barry</div><div class=""><br class=""></div><div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jul 30, 2022, at 2:42 PM, Edoardo alinovi <<a href="mailto:edoardo.alinovi@gmail.com" class="">edoardo.alinovi@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div dir="ltr" class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Barry, Jacob,</blockquote><div class=""><br class=""></div><div class="">Thank you very much for your clarification, I think the reference counting is what is preventing matDestroy() from releasing the matrix memory. </div><div class=""><br class=""></div><div class="">Just to give you a bit of context, in my CFD code I am solving momentum, pressure, plus two more equations for the turbulence model (k and omega). I have seen that allocating a matrix  for each equation and keep it in memory it's a bit optimistic for larger problems, so what I am trying to do is the following:</div><div class=""><br class=""></div><div class="">do itime=1,ntimes</div><div class=""><br class=""></div><div class="">momentum equation:</div><div class=""><br class=""></div><div class="">allocate the matrix</div><div class="">fill the matrix</div><div class="">ksp-solve()</div><div class="">matDestroy()</div><div class=""><br class=""></div><div class="">pressure equation </div><div class=""><div class="">allocate the matrix</div><div class="">fill the matrix</div><div class="">ksp-solve()</div><div class="">matDestroy()</div></div><div class=""><br class=""></div><div class="">k equation</div><div class=""><div class="">allocate the matrix</div><div class="">fill the matrix</div><div class="">ksp-solve()</div><div class="">matDestroy()</div></div><div class=""><br class=""></div><div class="">omega equation</div><div class=""><div class="">allocate the matrix</div><div class="">fill the matrix</div><div class="">ksp-solve()</div><div class="">matDestroy()</div></div><div class=""><br class=""></div><div class="">enddo</div><div class=""><br class=""></div><div class="">Matrices for momentum, pressure, k and omega have the same size but belong to different objects so that's why I have 4 of them :)</div><div class=""><br class=""></div><div class="">The idea I have in mind is to destroy A after its use for each equation ,so that the next equation matrix to be solved can pick up the memory freed from the previous one. At the end of the story I'll just allocate space for 1 matrix and not 4, or at least this is what I am trying to achieve. </div><div class=""><br class=""></div><div class="">The thing is I do not destroy the ksp, so maybe that detail is preventing my code from working as intended! </div><div class=""><br class=""></div></div></div>
</div></blockquote></div><br class=""></div></body></html>