# From FreeFem matrix to Petsc Mat, double size of memory usage?

**URL:** https://community.freefem.org/t/from-freefem-matrix-to-petsc-mat-double-size-of-memory-usage/4061
**Category:** General Discussion
**Created:** [September 9, 2025, 2:53pm UTC](https://community.freefem.org/t/from-freefem-matrix-to-petsc-mat-double-size-of-memory-usage/4061 "2025-09-09T14:53:47Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![MatrixVector](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@MatrixVector](https://community.freefem.org/u/MatrixVector)
#### Post date: [September 9, 2025, 2:53pm UTC](https://community.freefem.org/t/from-freefem-matrix-to-petsc-mat-double-size-of-memory-usage/4061/1 "2025-09-09T14:53:47Z")

</div>

In parallel study using PETSc, the examples in the directory examples/hpddm usually first generate the FreeFem matrix like

> matrix Aloc = vA(Vh, Vh);

“Aloc“, and then assign its value to a PETSc Mat like

> Mat A = Aloc;

When the problem size is very big, this means twice memory usage.

One alternative solution is to directly calculate the PETSc Mat like

> Mat A = vA(Vh, Vh);

This works for the case that all the Dirichlet boundary conditions can be enforced using the “on“ operator, but fails in the case pointwise Dirichlet condition exists.

So, when dealing with the pointwise Dirichlet condition, it seems the above two steps (first a FreeFem matrix, and then a PETSc Mat) is unavoidable.

Another way maybe first calculating the PETSc Mat without pointwise Dirichlet condition, and then using PETSc functions like “MatZeroRows“ to implement this pointwise Dirichlet condition. But this seems not supported by the current FreeFem++ code.

Anyone has other solutions? To solve the problem without twice memory usage.

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [September 9, 2025, 3:01pm UTC](https://community.freefem.org/t/from-freefem-matrix-to-petsc-mat-double-size-of-memory-usage/4061/2 "2025-09-09T15:01:21Z")

</div>

The cost of storing a sparse matrix is usually orders of magnitude lower than for storing a preconditioner (unless you are solving something trivial with a Jacobi preconditioner). That being said, you can free the memory used by the FreeFEM matrix by simply doing:  
`{ matrix empty(0, 0); Aloc = empty; }`

Then, there is no additional cost.

---

<div class="post-metadata">

### Author: ![MatrixVector](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@MatrixVector](https://community.freefem.org/u/MatrixVector)
#### Post date: [September 11, 2025, 9:22am UTC](https://community.freefem.org/t/from-freefem-matrix-to-petsc-mat-double-size-of-memory-usage/4061/3 "2025-09-11T09:22:40Z")

</div>

Dear Prof. Jolivet,

Thanks for your reply. The treatment of assigning the matrix “Aloc” as an empty matrix can release the memory usage. But, at the instant when we let “Mat A = Aloc“, the instaneous memory usage is still high, although it can be suddenly reduced when “Aloc = empty“ is used. That is, there still exists a moment that doubel size of memory usage is needed.

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [September 11, 2025, 9:38am UTC](https://community.freefem.org/t/from-freefem-matrix-to-petsc-mat-double-size-of-memory-usage/4061/4 "2025-09-11T09:38:39Z")

</div>

My point still stands. This is negligible in most applications, since the peak memory consumption will be reached when building the preconditioner, not the coefficient matrix. If it’s not in yours, please share more details.

---

<div class="post-metadata">

### Author: ![MatrixVector](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@MatrixVector](https://community.freefem.org/u/MatrixVector)
#### Post date: [September 11, 2025, 2:10pm UTC](https://community.freefem.org/t/from-freefem-matrix-to-petsc-mat-double-size-of-memory-usage/4061/5 "2025-09-11T14:10:26Z")

</div>

Thanks for your reply. If the peak memory consumption is utilized to build the preconditioner, then the “Aloc = empty“ way to free the memory is a good choice.
