# PHP memory limit with DB request loop

**URL:** <https://www.dolibarr.org/forum/t/php-memory-limit-with-db-request-loop/21506>\
**Category:** Developing for Dolibarr\
**Tags:** Code, Develop, v14, Database\
**Created:** [November 29, 2021, 3:54pm UTC](https://www.dolibarr.org/forum/t/php-memory-limit-with-db-request-loop/21506 "2021-11-29T15:54:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![masterit](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/masterit/32/4749_2.png) [@masterit](https://www.dolibarr.org/forum/u/masterit)\
**Post date:** [November 29, 2021, 3:54pm UTC](https://www.dolibarr.org/forum/t/php-memory-limit-with-db-request-loop/21506/1 "2021-11-29T15:54:47Z")

</div>

Hi,

I’d like to generate invoices from a `hook` and I got a php memory limit error. I don’t want to use the simple-but-sad solution of increasing the limit in my `php.ini`.

I got no problem to fetch all the invoice from my external api and store them into a beautiful `array()`, let’s call it `$invoices`.

I loop through each invoice in my `$invoices` array but even if I call `$db->commit()` after each loop, the php buffer seems to increase.

My code (simplified) :

```php

$invoices = array(...); // My array with all pre-fetched invoices

foreach ($invoices as $facture) { // Main loop
   $new_invoice = new Facture($db) // Invoice object creation

	[...] // Differents attributes of my $new_invoice object (Facture)

	foreach ($facture['lines'] as $line) {
		$line = new FactureLigne($db);

		[...] // Différents attributes de my $line object (FactureLigne) 
	}

	$id = $new_invoice->create($user); // Invoice creation (before payment)

	foreach ($facture['payments'] as $payment) {
		$new_payment = new Paiement($db);

		$new_payment->amount = $payment['amount'];
		$new_payment->amounts = array();
		$new_payment->amounts[$id] = $new_payment->amount; // facture <-> paiement link

		[...] // Differents attributes of my $new_payment object (Paiement) 
	}

	if ($id < 0) {
		$error++;
		dol_print_error($db, $new_invoice->error);
	} else {
		$new_invoice->validate($user);
		dol_syslog("Invoice created with id=" . $id . "\n", LOG_INFO);
		$db->commit(); // ---> HERE <----
	}
}

```

When I run this code and I look into the size of `$db->queries` when I try to commit (on the `---> HERE <---` comment), it seems to increase on each loop iteration of my main loop **without purge**.

How to modify / optimize this script to **unload memory** on each iteration?

Thanks for your help and sorry for my english.

Lucien

---

<div class="post-metadata">

**Author:** ![ksar](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/ksar/32/514_2.png) [@ksar](https://www.dolibarr.org/forum/u/ksar)\
**Post date:** [November 29, 2021, 4:08pm UTC](https://www.dolibarr.org/forum/t/php-memory-limit-with-db-request-loop/21506/2 "2021-11-29T16:08:10Z")

</div>

Hi,

Try a  
`$db->free(); `

---

<div class="post-metadata">

**Author:** ![masterit](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/masterit/32/4749_2.png) [@masterit](https://www.dolibarr.org/forum/u/masterit)\
**Post date:** [November 29, 2021, 4:28pm UTC](https://www.dolibarr.org/forum/t/php-memory-limit-with-db-request-loop/21506/3 "2021-11-29T16:28:37Z")

</div>

Hi @ksar,

Thanks for your quick answer

I tried your solution but doesn’t seems to work.

I put a `$db->free();` just after `$db->commit(); // ---> HERE <----` but still got memory limit exhausted.

---

<div class="post-metadata">

**Author:** ![masterit](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/masterit/32/4749_2.png) [@masterit](https://www.dolibarr.org/forum/u/masterit)\
**Post date:** [December 4, 2021, 2:18pm UTC](https://www.dolibarr.org/forum/t/php-memory-limit-with-db-request-loop/21506/4 "2021-12-04T14:18:58Z")

</div>

Some other ideas on how to paginate or free some memory… ?

---

<div class="post-metadata">

**Author:** ![masterit](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/masterit/32/4749_2.png) [@masterit](https://www.dolibarr.org/forum/u/masterit)\
**Post date:** [December 27, 2021, 9:17am UTC](https://www.dolibarr.org/forum/t/php-memory-limit-with-db-request-loop/21506/5 "2021-12-27T09:17:02Z")

</div>

Didn’t find any solution yet…
