[petsc-users] On MatDestroy() in Fortran

Barry Smith bsmith at petsc.dev
Sat Jul 30 13:26:14 CDT 2022


  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

   KSPSetOperators(  A)
   MatDestroy(A)
   KSPSolve()

will not free the memory because the KSP still has a reference to it. But doing

  KSPSetOperators( B)

or 

   KSPDestroy() 

will cause the matrix to be free since the KSP no longer has access to it. 

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. 

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 :-).

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.

Barry





> On Jul 30, 2022, at 1:55 PM, Edoardo alinovi <edoardo.alinovi at gmail.com> wrote:
> 
> Hello petsc friends,
> 
> I have a (silly?) question about matDestroy in Fortran, cause I am not sure I have got 100% clear what it is doing. 
> 
> 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? 
> 
> If the answer is yes, how can I force petsc to return all the memory used for A to the system? 
> 
> I found this post: https://lists.mcs.anl.gov/pipermail/petsc-users/2013-August/018466.html <https://lists.mcs.anl.gov/pipermail/petsc-users/2013-August/018466.html>, but cannot use that trick as I am not in C here. 
> 
> Thanks for the help as always! 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20220730/eac6261b/attachment.html>


More information about the petsc-users mailing list