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 repor
You can properly fix this by changing memory_limit
on fastcgi/fpm:
$vim /etc/php5/fpm/php.ini
Change memory, like from 128 to 512, see below
; Maximum amount of memory a script may consume (128 MB)
; http://php.net/memory-limit
memory_limit = 128M
to
; Maximum amount of memory a script may consume (128 MB)
; http://php.net/memory-limit
memory_limit = 512M
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 file 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.
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);
I spent two dyas looking a solution for this and I figured out that this was cause in a call with PDO when I called
$stmt->bindParam(":PERIOD", $period);
and the variables period was an
empty string ''
So this issue could have multiple root cause, my advice to you is that try a trial and error or a bisection method for a root cause finding, delete code and the try to search what is the line code that is failing
Update: I also faced this error with the method $pdo->query() I used $pdo->prepare() and worked well, so, while I had
$sql = "SELECT * FROM COURSE_DETAILS where ACTIVE = 1 AND COURSE_DETAILS_ID = $id";
$stmt = getConnection()->query($sql);
$courseDetails = $stmt->fetchAll(PDO::FETCH_ASSOC)
then i changed this to
$sql = "SELECT * FROM COURSE_DETAILS where ACTIVE = 1 AND COURSE_DETAILS_ID = ?";
$stmt = getConnection()->prepare($sql);
$stmt->execute(array($id));
and magically the memory error dissapeared!
When I removed the following lines from my code, all worked OK!
set_include_path(get_include_path() . get_include_path() . '/phpseclib');
include_once('Net/SSH2.php');
include_once('Net/SFTP.php');
These lines were included in every file I was running. When running the files one by one, all worked OK, but when running all files together I got the memory leak issue. Somehow the "include_once" is not including things once, or I am doing something wrong...
Running the script like this (cron case for example): php5 /pathToScript/info.php
produces the same error.
The correct way: php5 -cli /pathToScript/info.php