PHP error: Allowedmemorysizeofxxxbytes (2)

Source: Internet
Author: User
PHP error: Allowedmemorysizeofxxxbytes (2) in the previous article, the error "Allowed memory size of xxx bytes" occurs in php when querying data tables. during ini configuration, another function is used to solve this problem and exchange the memory space at the cost of some performance. in this article, it will explain the solution to this problem when reading files.

When reading files, file_get_contents () and file () are two common functions, because the operation will be less, I downloaded a m log file as the operation object.

// Here is the general writing method // $ file = file_get_contents ($ log_file); $ file = file ($ log_file); $ start = memory_get_usage (); $ end = memory_get_usage (); $ used = $ end-$ start; echo $ used;

Both file_get_contents and file have the same error as expected. "Fatal error: Allowed memory size of 134217728 bytes exhausted" is displayed ".

Now let's look at several other methods.

Code 1:

This is the result of reading a row every time using fgets (). However, if a row is large, an error message indicating insufficient memory is displayed.

Function test1 ($ log_file) {// memory: 664 bytes // Time: 13.063009977341 seconds $ fp = fopen ($ log_file, 'R + '); while (! Feof ($ fp) {// $ row = fgets ($ fp); $ row = fread ($ fp, 2048); echo $ row, "\ r \ n" ;}fclose ($ fp );}

Code 2:

The fread () function is used to control the size of data read each time. the memory usage and time consumption are directly related to the size of data read each time.

Function test3 ($ log_file) {// memory: 656 bytes // Time: 9.2053289413452 seconds $ fp = fopen ($ log_file, 'R + '); while (! Feof ($ fp) {$ row = fread ($ fp, 2048); // echo $ row;} fclose ($ fp );}

Code 3:

In this way, the file is opened using pipelines. compared with code 1, when fgets () is used, the memory usage is less than that in code 1.

Function test2 ($ log_file) {// memory: 352 bytes // Time: 13.38470697403 seconds $ cmd = "cat {$ log_file} 2> & 1 "; $ fp = popen ($ cmd, "r"); while (! Feof ($ fp) {$ row = fgets ($ fp); echo $ row, "\ r \ n" ;}pclose ($ fp );}


In addition, the fseek () function is also used, but all of them achieve the same purpose, that is, only the processed data is read into the memory to reduce memory usage.

You can also use php to call the system command to achieve the goal. this solution is not tested here.

Attach the function I used for testing:

$ Start = memory_get_usage (); $ time1 = microtime (true); test3 ($ log_file); $ end = memory_get_usage (); $ time2 = microtime (true ); $ used = $ end-$ start; $ limit = $ time2-$ time1; echo "memory:", $ used, "byte \ r \ n time:", $ limit, "Seconds \ r \ n ";

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.