<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Thank you for the precision, now i can eliminate this case, and
      try to find where my bug is coming from.</p>
    <p>Médane<br>
    </p>
    <div class="moz-cite-prefix">On 24/09/2021 17:21, Barry Smith wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:3A611848-D4C2-475A-94B2-C8066CBE1C21@petsc.dev">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <div class=""><br class="">
      </div>
        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. 
      <div class=""><br class="">
      </div>
      <div class="">  If you run the program with <span
          style="font-family: Menlo; font-size: 14px;" class="">-malloc_dump</span> then
        you will see nothing is printed since the memory used in the
        initialization is freed in PetscFinalize().</div>
      <div class=""><br class="">
      </div>
      <div class="">  Barry</div>
      <div class=""><br class="">
        <div><br class="">
          <blockquote type="cite" class="">
            <div class="">On Sep 24, 2021, at 10:31 AM, Medane TCHAKOROM
              <<a href="mailto:medane.tchakorom@univ-fcomte.fr"
                class="" moz-do-not-send="true">medane.tchakorom@univ-fcomte.fr</a>>
              wrote:</div>
            <br class="Apple-interchange-newline">
            <div class="">
              <div class="">Thanks Barry,<br class="">
                <br class="">
                I can't share the orginal code i'am working on
                unfortunately.<br class="">
                <br class="">
                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.<br class="">
                <br class="">
                Based on the example code i sent, i was expecting that
                PetscMallocDump give no output.<br class="">
                <br class="">
                Médane<br class="">
                <br class="">
                <br class="">
                On 24/09/2021 16:13, Barry Smith wrote:<br class="">
                <blockquote type="cite" class="">  The code you sent
                  looks fine, it should not leak memory.<br class="">
                  <br class="">
                   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.<br
                    class="">
                  <br class="">
                    We would need to see the full code to understand why
                  the matrix is not being freed.<br class="">
                  <br class="">
                    Barry<br class="">
                  <br class="">
                  <br class="">
                  <blockquote type="cite" class="">On Sep 24, 2021, at
                    10:08 AM, Medane TCHAKOROM <<a
                      href="mailto:medane.tchakorom@univ-fcomte.fr"
                      class="" moz-do-not-send="true">medane.tchakorom@univ-fcomte.fr</a>>
                    wrote:<br class="">
                    <br class="">
                    Hello,<br class="">
                    <br class="">
                    I have problem with a code i'am working on.<br
                      class="">
                    <br class="">
                    To illustrate my problem, here is an example:<br
                      class="">
                    <br class="">
                    <br class="">
                    int main(int argc, char *argv[])<br class="">
                    {<br class="">
                    <br class="">
                        PetscErrorCode ierr;<br class="">
                    <br class="">
                        ierr = PetscInitialize(&argc, &argv,
                    (char *)0, NULL);<br class="">
                        if (ierr)<br class="">
                            return ierr;<br class="">
                    <br class="">
                        int i = 0;<br class="">
                        for (i = 0; i < 1; i++)<br class="">
                        {<br class="">
                            Mat A;<br class="">
                            ierr = MatCreate(PETSC_COMM_WORLD, &A);<br
                      class="">
                            CHKERRQ(ierr);<br class="">
                            ierr = MatSetSizes(A, 16, 16,
                    PETSC_DECIDE,PETSC_DECIDE);<br class="">
                            CHKERRQ(ierr);<br class="">
                            ierr = MatSetFromOptions(A);<br class="">
                            CHKERRQ(ierr);<br class="">
                            ierr = MatSetUp(A);<br class="">
                            CHKERRQ(ierr);<br class="">
                    <br class="">
                    <br class="">
                            ierr = MatAssemblyBegin(A,
                    MAT_FINAL_ASSEMBLY);<br class="">
                            CHKERRQ(ierr);<br class="">
                            ierr = MatAssemblyEnd(A,
                    MAT_FINAL_ASSEMBLY);<br class="">
                            CHKERRQ(ierr);<br class="">
                    <br class="">
                    <br class="">
                    <br class="">
                            /// SOME CODE HERE....<br class="">
                    <br class="">
                            MatDestroy(&A);<br class="">
                    <br class="">
                    <br class="">
                        }<br class="">
                    <br class="">
                    <br class="">
                        FILE *fPtr;<br class="">
                        fPtr = fopen("petsc_dump_file.txt", "a");<br
                      class="">
                        PetscMallocDump(fPtr);<br class="">
                        fclose(fPtr);<br class="">
                    <br class="">
                        ierr = PetscFinalize();<br class="">
                        CHKERRQ(ierr);<br class="">
                    <br class="">
                        return 0;<br class="">
                    }<br class="">
                    <br class="">
                    <br class="">
                    <br class="">
                    The problem is , in the loop, the memory consumption
                    keep increasing till the end of the program.<br
                      class="">
                    <br class="">
                    I checked memory leak with PetscMallocDump, and
                    found out that the problem may be due to matrix
                    creation.<br class="">
                    <br class="">
                    I'am new to Petsc and i don't know if i'am doing
                    something wrong. Thanks<br class="">
                    <br class="">
                    <br class="">
                    Médane<br class="">
                    <br class="">
                  </blockquote>
                </blockquote>
              </div>
            </div>
          </blockquote>
        </div>
        <br class="">
      </div>
    </blockquote>
  </body>
</html>