简体   繁体   中英

Using ini_set('memory_limit') in a php function

I have a particularly memory intensive function I'd like to (just while that function is running) up the allowed memory for it to complete.

Is it poor practice to use ini_set('memory_limit' , '1024M') within a php function and once the function is completed will it return to default value?

I know it's a high value to use. It's a dedicated server and has plenty of juice.

Example would be:

function run_Cron_Processes() {
    ini_set('memory_limit', '1024M');
    memoryIntensive1();
    memoryIntensive2();
    memoryIntensive3();
    //return to default m limit
}
$oldLimit = ini_get( 'memory_limit' );
ini_set( 'memory_limit', '1024M' );
(...)
ini_set( 'memory_limit', $oldLimit );

But I think it is unnecessary: at the end of the script execution, memory limit is reset to default value.

The value will return to the default value after your script execution.

You can also set your limit to -1 in order to tell PHP that your script can use all the necessary memory.

I wouldn't call it is a poor practice, you got to do what you have to do. If you need more memory, that's the way to do it. You might also need to increase max_execution_time .

That being said, you might want to check your 'memoryIntensive' functions, and optimize things there.

ini_set('memory_limit','-1');

它将按要求使用内存,并且不会返回任何与内存端有关的错误或通知。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM