Asked  7 Months ago    Answers:  5   Viewed   36 times

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 13965430 bytes)

PHPInfo shows that I have a memory_limit of 128M, so I'm confused as to why the error says I only have 64M. Is it possible for phpinfo to report incorrectly? Or for PHP to use two separate php.inis?

The error was being caused by an ini_set call in one of the primary php files that a co-worker of mine added without my knowledge.



PHP's config can be set in multiple places:

  1. master system php.ini (usually in /etc somewhere)
  2. somewhere in Apache's configuration (httpd.conf or a per-site .conf file, via php_value)
  3. CLI & CGI can have a different php.ini (use the command php -i | grep memory_limit to check the CLI conf)
  4. local .htaccess files (also php_value)
  5. in-script (via ini_set())

In PHPinfo's output, the "Master" value is the compiled-in default value, and the "Local" value is what's actually in effect. It can be either unchanged from the default, or overridden in any of the above locations.

Also note that PHP generally has different .ini files for command-line and webserver-based operation. Checking phpinfo() from the command line will report different values than if you'd run it in a web-based script.

Wednesday, March 31, 2021
answered 7 Months ago

Here are three methods to increase the limit on shared hosting:

  1. Just add this below line to before line of you getting error in your file

     ini_set('memory_limit', '-1');
  2. If you have access to your PHP.ini file, change the line in PHP.ini If your line shows 32M try 64M: memory_limit = 64M ; Maximum amount of memory a script may consume (64MB)

  3. If you don't have access to PHP.ini try adding this to an .htaccess file: php_value memory_limit 64M

Also make sure your file pointer is valid, and pass "r" (= reading) for mode.

Saturday, May 29, 2021
answered 5 Months ago

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.

Tuesday, June 1, 2021
answered 5 Months ago

Ok, lets test this using a simple script:

ini_set('memory_limit', '1M');
$x = '';
while(true) {
  echo "not real: ".(memory_get_peak_usage(false)/1024/1024)." MiBn";
  echo "real: ".(memory_get_peak_usage(true)/1024/1024)." MiBnn";
  $x .= str_repeat(' ', 1024*25); //store 25kb more to string


not real: 0.73469543457031 MiB
real: 0.75 MiB

not real: 0.75910949707031 MiB
real: 1 MiB


not real: 0.95442199707031 MiB
real: 1 MiB

not real: 0.97883605957031 MiB
real: 1 MiB

PHP Fatal error:  Allowed memory size of 1048576 bytes exhausted (tried to allocate 793601 bytes) in /home/niko/test.php on line 7

Seems like real usage is the memory allocated from the system - which seems to get allocated in larger buckets than currently needed by the script. (I guess for performance reasons). This is also the memory the php process uses.

The $real_usage = false usage is the memory usage you actually used in your script, not the actual amount of memory allocated by Zend's memory manager.

Read this question for more information.

In short: to get how close are you to the memory limit, use $real_usage = true

Monday, June 28, 2021
answered 4 Months ago

The most memory efficient you'll get is probably by storing everything in a string, packed in binary, and use manual indexing to it.

$storage = '';

$storage .= pack('l', 42);

// ...

// get 10th entry
$int = current(unpack('l', substr($storage, 9 * 4, 4)));

This can be feasible if the "array" initialisation can be done in one fell swoop and you're just reading from the structure. If you need a lot of appending to the string, this becomes extremely inefficient. Even this can be done using a resource handle though:

$storage = fopen('php://memory', 'r+');
fwrite($storage, pack('l', 42));

This is very efficient. You can then read this buffer back into a variable and use it as string, or you can continue to work with the resource and fseek.

Sunday, August 8, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :