<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>  PETSc objects do use reference counting so if the reference count does not reach zero on a MatDestroy() the matrix is not actually freed, because somewhere else in the code there may be a reference for it that could cause it to be used. For example<div class=""><br class=""></div><div class="">   KSPSetOperators(  A)</div><div class="">   MatDestroy(A)</div><div class="">   KSPSolve()</div><div class=""><br class=""></div><div class="">will not free the memory because the KSP still has a reference to it. But doing</div><div class=""><br class=""></div><div class="">  KSPSetOperators( B)</div><div class=""><br class=""></div><div class="">or </div><div class=""><br class=""></div><div class="">   KSPDestroy() </div><div class=""><br class=""></div><div class="">will cause the matrix to be free since the KSP no longer has access to it. </div><div class=""><br class=""></div><div class="">When the reference count gets to zero the memory is freed with the system free() routine and thus is available for use by another malloc() call or (in theory) could be "returned to the OS" so another process could use it. In practice, I don't think the actual "returning to the OS" takes place until the process is exited. So you won't see the values in top for memory usage go down. </div><div class=""><br class=""></div><div class="">All of this is transparent and we don't expect users to do anything special; just try to destroy things as soon as they are no longer needed (but not sooner :-).</div><div class=""><br class=""></div><div class="">Is there a particular situation where you are running out of memory (or seem to be swapping memory) that is causing you difficulties? Perhaps we could suggest alternatives to allow you to run larger problems if we know a bit more about the situation.</div><div class=""><br class=""></div><div class="">Barry</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jul 30, 2022, at 1:55 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="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr" class=""><div dir="ltr" class=""><div class="">Hello petsc friends,</div><div class=""><br class=""></div><div class="">I have a (silly?) question about matDestroy in Fortran, cause I am not sure I have got 100% clear what it is doing. </div><div class=""><br class=""></div><div class="">I am asking this question because I have the feeling that matDestroy(A, ierr) is not returning the memory to the system, but it prevents from memory leaks. Is that right? </div><div class=""><br class=""></div><div class="">If the answer is yes, how can I force petsc to return all the memory used for A to the system? </div><div class=""><br class=""></div><div class="">I found this post: <a href="https://lists.mcs.anl.gov/pipermail/petsc-users/2013-August/018466.html" class="">https://lists.mcs.anl.gov/pipermail/petsc-users/2013-August/018466.html</a>, but cannot use that trick as I am not in C here. <br class=""></div><div class=""><br class=""></div><div class="">Thanks for the help as always! </div><div class=""><br class=""></div><div class=""><br class=""></div></div></div></div></div>
</div></blockquote></div><br class=""></div></body></html>