[petsc-users] On MatDestroy() in Fortran

Jacob Faibussowitsch jacob.fai at gmail.com
Sat Jul 30 13:27:46 CDT 2022


The post you mention conflates 2 concepts:

1. Implementation details of libc memory allocators
2. Internal reference counting in PETSc

So in order:

1. When a program calls free() on memory they previously allocated there is no guarantee that such memory is returned immediately to the operating system. The implementation (i.e. your compilers libc) is free to reuse this memory the next time you call malloc() in order to speed this up. The only way to 100% guarantee that memory is returned is to end the program.

2. PETSc implements reference counting so that multiple other objects can hold a pointer (reference) to another object. This is an optimization technique to avoid potentially costly copying. When you call <ObjectName>Destroy() it decrements the reference counter, if the count drops to 0 we call free() on it.

Best regards,

Jacob Faibussowitsch
(Jacob Fai - booss - oh - vitch)

> On Jul 30, 2022, at 13:55, 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, but cannot use that trick as I am not in C here. 
> 
> Thanks for the help as always! 
> 
> 



More information about the petsc-users mailing list