<div dir="ltr"><span style="font-size:12.8000001907349px">Hi,</span><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px">I have the below code section, every time it runs through this section, used memory increases very much and not decreases then. Could you please let me know what object did I forgot to release in the for-loop?</div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px">Many thanks.</div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px">PVS</div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px"><div><br></div><div>  int mu_x[20]={0,1,0,0,2,0,0,1,0,1,3,0,0,2,2,1,0,1,0,1};</div><div>  int mu_y[20]={0,0,1,0,0,2,0,1,1,0,0,3,0,1,0,2,2,0,1,1};</div><div>  int mu_z[20]={0,0,0,1,0,0,2,0,1,1,0,0,3,0,1,0,1,2,2,1};</div><div>  for (int ib=0;ib<isur->BIPoints.N;ib++)</div><div>  {</div><div>    Mat W,V;</div><div>    int Mm = isur->iSolidCellsNearBIPoint[ib+1] - isur->iSolidCellsNearBIPoint[ib];</div><div>    PetscScalar val=0;</div><div>    MatCreateSeqAIJ(PETSC_COMM_SELF,Mm,Mm,Mm,NULL,&W);</div><div>    MatCreateSeqAIJ(PETSC_COMM_SELF,Mm,L3D,L3D,NULL,&V);</div><div>    double xm, ym, zm, dm;</div><div>    for (int i=0;i<Mm;i++)</div><div>    {</div><div>      xm = Mesh->Cells[isur->SolidCellsNearBIPoint[isur->iSolidCellsNearBIPoint[ib]+i]].Cen.xyz[0] - isur->BIPoints.x[ib];</div><div>      ym = Mesh->Cells[isur->SolidCellsNearBIPoint[isur->iSolidCellsNearBIPoint[ib]+i]].Cen.xyz[1] - isur->BIPoints.y[ib];</div><div>      zm = Mesh->Cells[isur->SolidCellsNearBIPoint[isur->iSolidCellsNearBIPoint[ib]+i]].Cen.xyz[2] - isur->BIPoints.z[ib];</div><div>      dm = sqrt(xm*xm+ym*ym+zm*zm);</div><div>      val = WeightFunc(dm,isur->R_solid[ib]);</div><div>      MatSetValue(W,i,i,val,INSERT_VALUES);</div><div>    }</div><div>    MatAssemblyBegin(W,MAT_FINAL_ASSEMBLY);</div><div>    MatAssemblyEnd(W,MAT_FINAL_ASSEMBLY);</div><div> </div><div>    for (int i=0;i<Mm;i++)</div><div>    for (int j=0;j<L3D;j++)</div><div>    {</div><div>      xm = Mesh->Cells[isur->SolidCellsNearBIPoint[isur->iSolidCellsNearBIPoint[ib]+i]].Cen.xyz[0] - isur->BIPoints.x[ib];</div><div>      ym = Mesh->Cells[isur->SolidCellsNearBIPoint[isur->iSolidCellsNearBIPoint[ib]+i]].Cen.xyz[1] - isur->BIPoints.y[ib];</div><div>      zm = Mesh->Cells[isur->SolidCellsNearBIPoint[isur->iSolidCellsNearBIPoint[ib]+i]].Cen.xyz[2] - isur->BIPoints.z[ib];</div><div>      val = power(xm,mu_x[j])*power(ym,mu_y[j])*power(zm,mu_z[j]);</div><div>      MatSetValue(V,i,j,val,INSERT_VALUES);</div><div>    }</div><div>    MatAssemblyBegin(V,MAT_FINAL_ASSEMBLY);</div><div>    MatAssemblyEnd(V,MAT_FINAL_ASSEMBLY);</div><div>    //if(ib==13) MatView(V,PETSC_VIEWER_STDOUT_SELF);</div><div><br></div><div>    /// Solve least square problem:</div><div>    Mat Amat,W2,VtW2,VtW2V,invVtW2V, VtW2V1Vt,I,invVtW2V_b;</div><div><br></div><div>    MatDuplicate(W,MAT_DO_NOT_COPY_VALUES,&W2);</div><div>    MatDuplicate(W2,MAT_DO_NOT_COPY_VALUES,&VtW2);</div><div>    MatDuplicate(W2,MAT_DO_NOT_COPY_VALUES,&VtW2V);</div><div><br></div><div>    MatMatMult(W,W,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&W2);</div><div><br></div><div>    MatTransposeMatMult(V,W2,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&VtW2);</div><div><br></div><div>    MatMatMult(VtW2,V,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&VtW2V);</div><div><br></div><div>    MatCreateSeqDense(PETSC_COMM_SELF,L3D,L3D,NULL,&I);</div><div>    MatDuplicate(I,MAT_DO_NOT_COPY_VALUES,&invVtW2V);</div><div><br></div><div>    Vec ivec;</div><div>    VecCreateSeq(PETSC_COMM_SELF,L3D,&ivec);</div><div>    VecSet(ivec,1.0);</div><div>    MatDiagonalSet(I,ivec,INSERT_VALUES);</div><div><br></div><div>    IS rperm,cperm;</div><div>    MatFactorInfo info;</div><div>    MatGetOrdering(VtW2V,  MATORDERINGNATURAL,  &rperm,  &cperm);</div><div>    MatFactorInfoInitialize(&info);</div><div>    MatLUFactor(VtW2V, rperm, cperm, &info);</div><div><br></div><div>    MatMatSolve(VtW2V,I,invVtW2V);</div><div><br></div><div>    MatConvert(invVtW2V,MATSEQAIJ,MAT_INITIAL_MATRIX,&invVtW2V_b);</div><div>    MatMatTransposeMult(invVtW2V_b,V,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&VtW2V1Vt);</div><div>    MatMatMult(VtW2V1Vt,W2,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&Amat);</div><div>    for (int i=0;i<L3D;i++)</div><div>    for (int j=0;j<Mm;j++)</div><div>      MatGetValues(Amat,1,&i,1,&j,&A[ib][i][j]);</div><div><br></div><div>    MatDestroy(&W2);</div><div>    MatDestroy(&Amat);</div><div>    MatDestroy(&VtW2);</div><div>    MatDestroy(&VtW2V);</div><div>    MatDestroy(&invVtW2V);</div><div>    MatDestroy(&invVtW2V_b);</div><div>    MatDestroy(&VtW2V1Vt);</div><div>    MatDestroy(&I);</div><div><br></div><div>    MatDestroy(&W);</div><div>    MatDestroy(&V);</div><div>    VecDestroy(&ivec);</div><div>  }</div></div></div>