- 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:
- 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.
- 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
- At the top of the file it working fine. we do not know if php has a memory leak.
The program is very simple:
- 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:
For Drupal users, this is the solution:
works but we need to put it just after the opening
tag in the index.php file in your site’s root directory.
Your site’s root directory:
[ad type=”banner”]You can properly fix this by changing memory_limit on fastcgi/fpm
Change memory like from 128 to 512:
memory_limit = 128M
memory_limit = 512M