#1154 – setting PHP memory_limit to 512M without checking is an increase

Status: Pending - Category: Econa
Hello,

We have developed some front end triggered admin cache cleaning and rebuilding utilities that require a large increase in memory (1G). These utilities are only triggered by admins and the memory is only increased when admin clicks the button so it is not a performance issue.

Our memory increase was not taking place as expected and we realized that the Econa plugin is forcing the memory_limit to 512M without regard for whether this is an increase of memory or not.

I could not find online examples for best practices to increase memory without risk of decreasing it but the snippet below should work to replace your
ini_set('memory_limit', '512M');
call with a check for existing value.

$memory_limit = ini_get('memory_limit');
$memory_num   = preg_replace('#^(\d+).*#', '$1', $memory_limit);
if (str_contains($memory_limit, 'K')) {
	$memory_num *= 1024;
}
if (str_contains($memory_limit, 'M')) {
	$memory_num *= 1024 * 1024;
}
if (str_contains($memory_limit, 'G')) {
	$memory_num *= 1024 * 1024 * 1024;
}
if ($memory_num < (512 * 1024 * 1024)) {
	ini_set('memory_limit', '512M');
}


I left multiples of 1024 just for clarity for kilobyte, megabyte and gigabyte values.

please let us know if this can be rolled into the next release so we do not need to maintain a patch.

Thank you
Hi Luke,

Can you please provide more details? Is your code placed in a component? A plugin? You can just override this right in your code by simply setting it to the desired value:
ini_set('memory_limit', '1024M');


The latest ini_set command will be the one that is applied.
 
Hi Lefteris,

Sorry for the delay. i missed your response dealing with some deadlines.

on to your questions, we are setting the memory limit in both a joomla registered library we have created and in a CLI console plugin.

The problem is twofold. first, econa is setting the limit when the plugin is registered and not when it is fired meaning that is lowering the limit midway during our page building process. We could increase it after econa decreases it but the damage would already be done if the process was over 512MB. The second issue is that econa is (presumably) trying to increase the memory but this is being done without checking the current limit so econa is blindly changing limit without knowing if it is necessary and in a case like ours actually lowering it causing an error.

I know yours is not the first 3rd party extension to do this but I think best practices would be to only change the memory limit if it is currently lower than what your extension requires. also, modifying the memory limit just because your extension is loaded is overkill -- it should only change the limit when it is actually fired.

Maybe a more general solution would be a wrapper that increases the limit based on current limit and max server limit.

Hope this clarifies a bit and explains how our ini_set is being overwritten by econa's.

Oliver Rockwell
Hi Oliver,

Thank you for clarifying. We are actively working on the new major version of Econa which will be fully Joomla 5 compatible.
I have already moved the ini_set call inside the function so it is only executed when it's truly needed.

I will consider the snippet you provided but maybe the best solution overall would be to remove completely the ini_set call.

Thank you very much for the feedback you have provided.

Regards
 

Note: An active subscription is required in order to get support for our paid extensions. For our free extensions, if you don't have an account, register and then submit your support request.  In case you just want to ask a question, you can also use the contact form .

Note: Our support team is currently on vacation, so please anticipate slightly longer response times. Thank you for your understanding!

Firecoders
Are you using our extensions? Please post a review at the Joomla extensions directory!
Post a review