• We have a bunch of client Point Of Sale (POS) systems that periodically send new sales data to one centralized database, which stores the data into one big database for report generation.
  • The client POS is based on PHP POS, and we have implemented a module that uses the standard XML-RPC library to send sales data to the service. The server system is built on CodeIgniter, and uses the XML-RPC and XML-RPCS libraries for the webservice component. Whenever we send a lot of sales data (as little as 50 rows from the sales table, and individual rows from sales_items pertaining to each item within the sale) we get the following error:
[pastacode lang=”php” manual=”Fatal%20error%3A%20Allowed%20memory%20size%20of%20134217728%20bytes%20exhausted%20(tried%20to%20allocate%2054%20bytes)%0A” message=”Php Code” highlight=”” provider=”manual”/]
  • 128M is the default value in php.ini, but we assume that is a huge number to break. In fact, we have even tried setting this value to 1024M, and all it does is take a longer time to error out.
  • As for steps we’ve taken for disabling all processing on the server-side, and have rigged it to return a canned response regardless of the input. However, we believe the problem lies in the actual sending of the data and tried disabling the maximum script execution time for PHP, and it still errors out.

  • People, changing the memory_limit by ini_set(‘memory_limit’, ‘-1’); is NOT a solution at all.
  • Don’t do that. Obviously php has a memory leak somewhere and you are telling the server to just use all the memory that it wants.
  • The problem has not been fixed at all. If you monitor your server, you will see that it is now probably using up most of the RAM and even swapping to disk.
  • You should probably try to track down the exact bug in your code and fix it.
[ad type=”banner”]

  • The correct way is to edit your php.ini file. Edit memory_limit to your desire value.
  • As from your question, 128M (which is the default limit) has been exceeded, so there is something seriously wrong with your code as it should not take that much.
  • If you know why it takes that much and you want to allow it set memory_limit = 512M or higher and you should be good.

  • The memory allocation for PHP can be adjusted permanently, or temporarily.

Permanent: 

  • You can permanently change the PHP memory allocation two ways.
  • If you have access to your php.ini file, you can edit the value for memory_limit to your desire value.
  • If you do not have access to your php.ini file (and your webhost allows it), you can override the memory allocation through your .htaccess file. Add php_value memory_limit 128M (or whatever your desired allocation is)

Temporary:

  • You can adjust the memory allocation on the fly from within a PHP file. You simply have the code ini_set(‘memory_limit’, ‘128M’); (or whatever your desired allocation is).
  • You can remove the memory limit (although machine or instance limits may still apply) by setting the value to “-1”.

When adding 22.5 million records into an array with array_push we getting “memory exhausted” fatal errors at around 20M records using 4G as the memory limit in php.ini.
To fix this we added the statement

[pastacode lang=”php” manual=”%24old%20%3D%20ini_set(‘memory_limit’%2C%20’8192M’)%3B%20%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]
  • At the top of the file it working fine. we do not know if php has a memory leak.

The program is very simple:

[pastacode lang=”php” manual=”%24fh%20%3D%20fopen(%24myfile)%3B%0Awhile%20(!feof(%24fh))%20%7B%0A%20%20%20%20%20%20array_push(%24file%2C%20stripslashes(fgets(%24fh)))%3B%0A%7D%20%20%0Afclose(%24fh)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]
  • The fatal error points to line 3 until i boosted the memory limit, which eliminated the error.

EDIT: Corrected the syntax of ini_set.

When you see the above error – especially if the (tried to allocate __ bytes) is a low value, that could be an indicator of an infinite loop, like a function that calls itself with no way out:

[pastacode lang=”php” manual=”function%20exhaustYourBytes()%0A%7B%0A%20%20%20%20return%20exhaustYourBytes()%3B%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

For Drupal users, this is the solution:

[pastacode lang=”php” manual=”ini_set(‘memory_limit’%2C%20′-1’)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]

works but we need to put it just after the opening

[pastacode lang=”php” manual=”%3C%3Fphp%0A” message=”Php Code” highlight=”” provider=”manual”/]

tag in the index.php file in your site’s root directory.

Your site’s root directory:

[pastacode lang=”php” manual=”ini_set(‘memory_limit’%2C%20’1024M’)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

You can properly fix this by changing memory_limit on fastcgi/fpm

[pastacode lang=”php” manual=”%24vim%20%2Fetc%2Fphp5%2Ffpm%2Fphp.ini%0A” message=”Php Code” highlight=”” provider=”manual”/]

Change memory like from 128 to 512:

memory_limit = 128M

[pastacode lang=”php” manual=”%3B%20Maximum%20amount%20of%20memory%20a%20script%20may%20consume%20(128MB)%0A%3B%20http%3A%2F%2Fphp.net%2Fmemory-limit%0Amemory_limit%20%3D%20128M%0A” message=”Php Code” highlight=”” provider=”manual”/]

memory_limit = 512M

[pastacode lang=”php” manual=”%3B%20Maximum%20amount%20of%20memory%20a%20script%20may%20consume%20(128MB)%0A%3B%20http%3A%2F%2Fphp.net%2Fmemory-limit%0Amemory_limit%20%3D%20512M%0A” message=”Php Code” highlight=”” provider=”manual”/]

Categorized in: