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

Barry Smith bsmith at petsc.dev
Fri Sep 24 09:13:59 CDT 2021


 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
> 



More information about the petsc-users mailing list