Fatal error:allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes), allocate2611816_php tutorial

Source: Internet
Author: User

Fatal error:allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes), allocate2611816


Today to use PHP code to process a 580M log file, a total of more than 2.19 million lines of records, because it is a. log file, under Windows It is difficult to separate files according to the number of bars, so under Linux with split-l 10000 filename prefix name The entire file is divided into 200 small files by 10000 lines, then using PHP to cycle through the 200 files, but the implementation of the following error occurred on the topic:

Copy the Code code as follows:
Fatal error:allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes)

To Baidu, the original is php.ini in the memory allocation problem, the default PHP code can apply to the maximum number of bytes of memory is 134217728 bytes, if the code executes when more memory needs, will be error, so the php.ini file configuration changed a bit:

Copy the Code code as follows:
Memory_limit = 128m;//changed 128M to 256M

But then, a PHP script to request the memory space will be more than 128M, that no matter how much you will be memory_limit later, there must be a problem in the future.

The reason is that when I encode, I only assign values to variables, but never unset ($var). Cause the memory to occupy more and more, so after a variable is no longer used, be sure to remember to unset it off.

The following is the code that I handled this log file today:

Copy CodeThe code is as follows:
<?php
Set_time_limit (1800);
/**
* Get the email address in the log that failed to send
* @param $directory Log log directory
* File name saved @param $name failed mailbox
*/
function Getmail ($directory, $name) {
Traverse the. log file in the directory
$files =scandir ("$directory");
foreach ($files as $v) {
if (Preg_match_all ("|mail\.log\d+|", $v, $log)) {
$logs []= $log [0][0];
}
}
Extract the failed mailboxes sent from all. log files
foreach ($logs as $v) {
$row =file ("$v");
echo "read". $v. " File
";
foreach ($row as $key = $value)
{
if (eregi ("Host name lookup failure| Connection timed out with| Connection refused By|cannot find your reverse hostname ", $value)) {
if (Preg_match ("|\w+" ([-+.] \w+) *@\w+ ([-.] \w+) *.\w+ ([-.] \w+) *| ", $row [$key], $matches)) {
$mail [] = Trim ($matches [0]);
echo "Get email address failed to send". $matches [0]. "
";
}else{
echo "Could not get the mailbox that failed to send in the log, please check";
}
}
}
Unset ($row);
}
Writes the extracted send failed mailbox to the Mail.txt file
$mailurl =fopen ("$name", "a");
foreach ($mail as $line)
{
Fwrite ($mailurl, $line. " \ r \ n ");
}
echo "writes all mailbox addresses that failed to send". $name. "
";
Fclose ($mailurl);
}

Getmail (".", "Mail.txt");
?>

http://www.bkjia.com/PHPjc/908177.html www.bkjia.com true http://www.bkjia.com/PHPjc/908177.html techarticle Fatal error:allowed Memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes), allocate2611816 today with ph P code to process a 580M log file, a total of ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.