[petsc-users] Petsc memory consumption keep increasing in my loop

Barry Smith bsmith at petsc.dev
Fri Sep 24 10:21:23 CDT 2021


  Ahh, the stuff you are seeing is just memory associated with the initialization of the matrix package; it is not the matrix memory (that is all freed). This memory used in the initialization is used only once and will not grow with more matrices. 

  If you run the program with -malloc_dump then you will see nothing is printed since the memory used in the initialization is freed in PetscFinalize().

  Barry


> On Sep 24, 2021, at 10:31 AM, Medane TCHAKOROM <medane.tchakorom at univ-fcomte.fr> wrote:
> 
> Thanks Barry,
> 
> I can't share the orginal code i'am working on unfortunately.
> 
> But the example i wrote -- even if you do not that into account //SOME CODE HERE .. part -- give me , by using PetscMallocDump, some informations about memory that was not freed.
> 
> Based on the example code i sent, i was expecting that PetscMallocDump give no output.
> 
> Médane
> 
> 
> On 24/09/2021 16:13, Barry Smith wrote:
>>  The code you sent looks fine, it should not leak memory.
>> 
>>  Perhaps the /// SOME CODE HERE.... is doing something that prevents the matrix from being actually freed. PETSc uses reference counting on its objects so if another object keeps a reference to the matrix then the memory of the matrix will not be freed until the reference count drops back to zero. For example if a KSP has a reference to the matrix and the KSP has not been completely freed the matrix memory will remain.
>> 
>>   We would need to see the full code to understand why the matrix is not being freed.
>> 
>>   Barry
>> 
>> 
>>> On Sep 24, 2021, at 10:08 AM, Medane TCHAKOROM <medane.tchakorom at univ-fcomte.fr> wrote:
>>> 
>>> Hello,
>>> 
>>> I have problem with a code i'am working on.
>>> 
>>> To illustrate my problem, here is an example:
>>> 
>>> 
>>> int main(int argc, char *argv[])
>>> {
>>> 
>>>     PetscErrorCode ierr;
>>> 
>>>     ierr = PetscInitialize(&argc, &argv, (char *)0, NULL);
>>>     if (ierr)
>>>         return ierr;
>>> 
>>>     int i = 0;
>>>     for (i = 0; i < 1; i++)
>>>     {
>>>         Mat A;
>>>         ierr = MatCreate(PETSC_COMM_WORLD, &A);
>>>         CHKERRQ(ierr);
>>>         ierr = MatSetSizes(A, 16, 16, PETSC_DECIDE,PETSC_DECIDE);
>>>         CHKERRQ(ierr);
>>>         ierr = MatSetFromOptions(A);
>>>         CHKERRQ(ierr);
>>>         ierr = MatSetUp(A);
>>>         CHKERRQ(ierr);
>>> 
>>> 
>>>         ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
>>>         CHKERRQ(ierr);
>>>         ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
>>>         CHKERRQ(ierr);
>>> 
>>> 
>>> 
>>>         /// SOME CODE HERE....
>>> 
>>>         MatDestroy(&A);
>>> 
>>> 
>>>     }
>>> 
>>> 
>>>     FILE *fPtr;
>>>     fPtr = fopen("petsc_dump_file.txt", "a");
>>>     PetscMallocDump(fPtr);
>>>     fclose(fPtr);
>>> 
>>>     ierr = PetscFinalize();
>>>     CHKERRQ(ierr);
>>> 
>>>     return 0;
>>> }
>>> 
>>> 
>>> 
>>> The problem is , in the loop, the memory consumption keep increasing till the end of the program.
>>> 
>>> I checked memory leak with PetscMallocDump, and found out that the problem may be due to matrix creation.
>>> 
>>> I'am new to Petsc and i don't know if i'am doing something wrong. Thanks
>>> 
>>> 
>>> Médane
>>> 

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


More information about the petsc-users mailing list