Fatal Error: Allowed Memory Size of 134217728 Byte

2018-12-31 03:13发布

I 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 PHPPOS, and I 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 I 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) I get the following error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 54 bytes)

128M is the default value in php.ini, but I assume that is a huge number to break. In fact, I have even tried setting this value to 1024M, and all it does is take a longer time to error out.

As for steps I've taken, I've tried disabling all processing on the server-side, and have rigged it to return a canned response regardless of the input. However, I believe the problem lies in the actual sending of the data. I've even tried disabling the maximum script execution time for PHP, and it still errors out.

23条回答
栀子花@的思念
2楼-- · 2018-12-31 03:59

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.

查看更多
情到深处是孤独
3楼-- · 2018-12-31 03:59

Your site's root directory:-

ini_set('memory_limit', '1024M');
查看更多
情到深处是孤独
4楼-- · 2018-12-31 03:59

CRASH page? enter image description here

(It happens when MySQL has to query large rows, by default the momory_limit is set to small, which was safer for the hardware)

You can check your system existing memory status, before increasing php.ini

# free -m
             total       used       free     shared    buffers     cached
Mem:         64457      63791        666          0       1118      18273
-/+ buffers/cache:      44398      20058
Swap:         1021          0       1021

Here i have increased it as following and then service httpd restart to fix the CRASH Page issue.

# grep memory_limit /etc/php.ini
memory_limit = 512M
查看更多
栀子花@的思念
5楼-- · 2018-12-31 04:00

Rather than changing the memory_limit value in your php.ini file, if there's a part of your code that could use a lot of memory, you could remove the memory_limit before that section runs, and then replace it after.

$limit = ini_get('memory_limit');
ini_set('memory_limit', -1);
// ... do heavy stuff
ini_set('memory_limit', $limit);
查看更多
几人难应
6楼-- · 2018-12-31 04:01

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

$old = ini_set('memory_limit', '8192M'); 

at the top of the file. Now everything is working fine. I do not know if php has a memory leak, that is not my job, nor do i care. I just have to get my job done, and this worked.

The program is very simple:

$fh = fopen($myfile);
while (!feof($fh)) {
      array_push($file, stripslashes(fgets($fh)));
}  
fclose($fh);

The fatal error points to line 3 until i boosted the memory limit, which eliminated the error.

查看更多
永恒的永恒
7楼-- · 2018-12-31 04:02

PHP 5.3+ allows you to change the memory limit by placing a .user.ini file in the public_html folder. Simply create the above file and type the following line in it:

memory_limit = 64M

Some cPanel hosts only accept this method.

查看更多
登录 后发表回答