I've a script on Drupal7 that generates a memory leak with this error:
PHP Fatal error: Allowed memory size of 314572800 bytes exhausted (tried to allocate 72 bytes) in /var/www/myprogram/includes/entity.inc on line 415

This script reads a cvs file, creates an user for each line and modifies some properties of the user added with profile2.
The code that generates the problem is this:

foreach(...){
	.....
	... <user creation ok>
	.....
	$account = user_load_by_mail($customerEmail);
	$profile = profile2_load_by_user($account, NULL);
	if (!empty($profile)){
		// In this $profile, update your fields you want.
		$profile["customer_account"]->field_company_name[LANGUAGE_NONE][0]["value"] = $customerRagSoc;
		$profile["customer_account"]->field_piva[LANGUAGE_NONE][0]["value"] = $customerPiva;
		// save user profile
		profile2_save($profile["customer_account"]) ;
	}
	else {
		error_log("......");
	}
	...
	...
}

Executing these lines (the lines that update the profile2 properties) the script acquires memory on each cycle without releasing it.
If I comment this code the quantity of memory used by the script is stable and there isn't a memory leak.

Where is the problem in those lines according to you?
I won't modify the parameter memory_limit in php.ini.

Thank you very much

cld

Comments

nitin.k’s picture



foreach(...){
	.....
	... <user creation ok>
	.....
	$account = user_load_by_mail($customerEmail); 
	$profile = profile2_load_by_user($account, NULL); // This is consuming more memory
	if (!empty($profile)){
		// In this $profile, update your fields you want.
		$profile["customer_account"]->field_company_name[LANGUAGE_NONE][0]["value"] = $customerRagSoc;
		$profile["customer_account"]->field_piva[LANGUAGE_NONE][0]["value"] = $customerPiva;
		// save user profile
		profile2_save($profile["customer_account"]) ; // This is consuming more memory
	}
	else {
		error_log("......");
	}
	...
	...
}


Let me tell you the trick..
We need to debug it effectively. We need to off the blacked functions one by one and observe the scenario. Profile functions which you are calling must consume more memory as compared to user_load_by_mail function.

ilclaudio’s picture

Yes I've done a bit of debugging.
The problem is in "user_load_by_mail".
This is the function that takes memory without releasing it.
I've discovered it removing all the other lines, the problem persists if I use other functions to load the account:
$account = user_load($userUid);
or directly:
$profile = profile2_by_uid_load($userUid, NULL);
that I suppose loads the account before loading the profile2 object.

It seeems that those functions load all the user data but don't release memory if I put $account=null or unset($account).
So I agree that loading an account is an activity that consumes memory, but after using it I need to release the memory.

Claudio