PHP long-running memory overflow.

Source: Internet
Author: User
Keywords Php
Loop a TXT file under a folder where the data is read by row into the database appears Fatal error:allowed memory size of 134217728 bytes exhausted error message,

The code below, I each TXT file is not big (about 100k), and is read by the line, why?

        $file = new \FilesystemIterator($fileName);        foreach ($file as $fileinfo) {            if ( $fileinfo->isFile() ) {                $fp = fopen($fileName . '/' . $fileinfo->getFilename(), 'r');                while(! feof($fp)) {                    $data['score'] = 7;                    $data['code'] = fgets($fp);                    $this->book->add($data);//存进数据库                }                fclose($fp);            }        }

Reply content:

Loop a TXT file under a folder where the data is read by row into the database appears Fatal error:allowed memory size of 134217728 bytes exhausted error message,

The code below, I each TXT file is not big (about 100k), and is read by the line, why?

        $file = new \FilesystemIterator($fileName);        foreach ($file as $fileinfo) {            if ( $fileinfo->isFile() ) {                $fp = fopen($fileName . '/' . $fileinfo->getFilename(), 'r');                while(! feof($fp)) {                    $data['score'] = 7;                    $data['code'] = fgets($fp);                    $this->book->add($data);//存进数据库                }                fclose($fp);            }        }

Iterative operation with keyword yield does not store intermediate variables to save memory and requires php5.5+

$this->book->add ($data);
Unset ($data);

While looping, $data will occur when write-time replication (COW), which will not be released until the end of your script execution

Install Xdebug or xhprof to view performance analysis reports.

I'm estimating the configuration of the framework, SQL did a collection

  • 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.