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

Source: Internet
Author: User
Tags log log

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 by the number of bars, so under Linux with split-l 10000 filename prefix name The entire file by 10000 lines a split into more than 200 small files, and then use PHP to loop through the more than 200 files, but the implementation of the later appeared on the subject of the error:

Copy Code code as follows:

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

To Baidu a bit, 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 to execute the time needed more memory, will be an error, so the php.ini file configuration changed a bit:

Copy Code code as follows:

Memory_limit = 128m;//changed 128M to 256M

But after a thought, a PHP script to request more than 128M of memory space, that no matter how much you will be memory_limit set up later, there must be a problem.

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

Attached below is the code for the log file I am working on today:

Copy Code code as follows:

<?php
Set_time_limit (1800);
/**
* Get the email address in the log that failed to send
* @param directory $directory log log
* @param file name saved by $name failed mailbox
*/
function Getmail ($directory, $name) {
Traverse the. log file under 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 from all. log files
foreach ($logs as $v) {
$row =file ("$v");
echo "read". $v. " Document &LT;BR/> ";
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 "Gets the mailbox address that failed to send". $matches [0]. " <br/> ";
}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 failed mailbox addresses to the. $name. <br/> ";
Fclose ($mailurl);
}

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

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.