Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php

前端 未结 21 1226
猫巷女王i
猫巷女王i 2020-11-21 22:14

This error message is being presented, any suggestions?

Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php

相关标签:
21条回答
  • 2020-11-21 22:59

    At last I found the answer:

    Just add this line before the line where you get error in your php file

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

    It will take unlimited memory usage of server, it's working fine.

    Consider '44M' instead of '-1' for safe memory usage.

    0 讨论(0)
  • 2020-11-21 22:59

    I had the same issue which running php in command line. Recently, I had changes the php.ini file and did a mistake while changing the php.ini

    This is for php7.0

    path to php.ini where I made mistake: /etc/php/7.0/cli/php.ini

    I had set memory_limit = 256 (which means 256 bytes)
    instead of memory_limit = 256M (which means 256 Mega bytes).

    ; Maximum amount of memory a script may consume (128MB)
    ; http://php.net/memory-limit
    memory_limit = 128M

    Once I corrected it, my process started running fine.

    0 讨论(0)
  • 2020-11-21 22:59

    if you are using laravel then use this ways

    public function getClientsListApi(Request $request){ print_r($request->all()); //for all request print_r($request->name); //for all name }

    instead of

    public function getClientsListApi(Request $request){ print_r($request); // it show error as above mention }

    0 讨论(0)
  • 2020-11-21 23:00

    We had a similar situation and we tried out given at the top of the answers ini_set('memory_limit', '-1'); and everything worked fine, compressed images files greater than 1MB to KBs.

    0 讨论(0)
  • 2020-11-21 23:01

    Your script is using too much memory. This can often happen in PHP if you have a loop that has run out of control and you are creating objects or adding to arrays on each pass of the loop.

    Check for infinite loops.

    If that isn't the problem, try and help out PHP by destroying objects that you are finished with by setting them to null. eg. $OldVar = null;

    Check the code where the error actually happens as well. Would you expect that line to be allocating a massive amount of memory? If not, try and figure out what has gone wrong...

    0 讨论(0)
  • 2020-11-21 23:05

    Doing :

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

    is never good. If you want to read a very large file, it is a best practise to copy it bit by bit. Try the following code for best practise.

    $path = 'path_to_file_.txt';
    
    $file = fopen($path, 'r');
    $len = 1024; // 1MB is reasonable for me. You can choose anything though, but do not make it too big
    $output = fread( $file, $len );
    
    while (!feof($file)) {
        $output .= fread( $file, $len );
    }
    
    fclose($file);
    
    echo 'Output is: ' . $output;
    
    0 讨论(0)
提交回复
热议问题