Search code examples
phpini-set

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
}

Solution

  • $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.