PHP memory limit with DB request loop


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) :

$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) {
		dol_print_error($db, $new_invoice->error);
	} else {
		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.



Try a

1 Like

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.

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

Didn’t find any solution yet…