The most comprehensive php memory overflow solution

Source: Internet
Author: User
The most comprehensive php memory overflow solution 1. memory overflow solution
During data statistical analysis, large arrays are often encountered, and memory overflow may occur. here is my solution. The problem is illustrated in the following example:
Assume that the number of records in the log is 500000, the solution is as follows:

The code is as follows:

Ini_set ('memory _ limit ', '64m'); // reset the memory size that php can use to 64 MB. generally, php cannot be modified on a remote host. the INI file can only be set through the program. Note: in safe_mode (security mode), ini_set fails set_time_limit (600 ); // Set the timeout limit to 6 minutes $ farr = $ Uarr = $ Marr = $ IParr = $ data =$ _ sub = array (); $ spt = "$ @#! $ "; $ Root ="/Data/webapps/VisitLog "; $ path = $ dpath = $ fpath = NULL; $ path = $ root. "/". date ("Y-m", $ timestamp); $ dpath = $ path. "/". date ("m-d", $ timestamp); for ($ j = 0; $ j <24; $ j ++) {$ v = ($ j <10 )? "0". $ j: $ j; $ gpath = $ dpath. "/". $ v. ". php"; if (! File_exists ($ gpath) {continue;} else {$ arr = file ($ gpath); // read the file into the array array_shift ($ arr ); // remove the first unit-"
 $ Farr = array_merge ($ farr, $ arr); unset ($ arr) ;}} if (empty ($ this-> farr) {echo"

No related records!

"; Exit;} while (! Empty ($ farr) {$ _ sub = array_splice ($ farr, 0, 10000); // 1000 for ($ I = 0, $ scount = count ($ _ sub); $ I <$ scount; $ I ++) {$ arr = explode ($ spt, $ _ sub [$ I]); $ Uarr [] = $ arr [1]; // vurl $ Marr [] = $ arr [2]; // vmark $ IParr [] = $ arr [3]. "| $ nbsp ;". $ arr [1]; // IP} unset ($ _ sub); // destroy immediately after use} unset ($ farr );

It is not difficult to see that, on the one hand, we need to increase the available memory size of PHP, on the other hand, as long as we want to try to batch process the array, divide and conquer it, and timely destroy the used variables (unset ), generally, no overflow occurs.

In addition, to reduce the memory loss of PHP programs, we should minimize the use of static variables. you can consider using references (&) when you need to re-use data (&). Another point is that the connection should be closed immediately after the database operation is completed. after an object is used up, the destructor (_ destruct () should be called in time ()).

II. unset: How to Destroy variables and release memory
The unset () function of PHP is used to clear and destroy variables. unused variables can be destroyed using unset. However, in some cases, the memory occupied by the volume change cannot be destroyed with unset! Let's first look at an example:

The code is as follows:

 

Finally, the output unset () occupies memory minus unset () and then occupies memory. if it is a positive number, it indicates that unset ($ s) has destroyed $ s from memory (or, after unset (), the memory usage is reduced. However, on the PHP5 and windows platforms, the result is: 0. Does this indicate that unset ($ s) does not play the role of destroying the memory occupied by the variable $ s? Let's take the following example:

The code is as follows:

 

In this example, it is almost the same as the preceding example. The only difference is that $ s is composed of 256 1 s, that is, one more than the first example. The result is 272. Does this mean that unset ($ s) has destroyed the memory occupied by $ s?
Through the above two examples, we can draw the following conclusions:
Conclusion 1: The unset () function only releases the memory space when the memory occupied by the variable value exceeds 256 bytes.
Is it possible to use unset to release memory space as long as the variable value exceeds 256? Let's test it through an example:

The code is as follows:

 '; Echo $ m-$ mm;?>

Refresh the page. we can see that there are 256 1 in the first line and 0 in the second line. we have destroyed $ s, and $ p is just a variable that references $ s, there should be no content. In addition, the memory usage before and after unset ($ s) remains unchanged! Now let's take the following example:

The code is as follows:

 '; Echo $ m-$ mm;?>

Refresh the page and we can see that the output $ p has no content. The difference between memory usage and unset () is 272, that is, the memory occupied by variables has been cleared. In this example, $ s = null can also be replaced with unset (), as follows:

The code is as follows:

 '; Echo $ m-$ mm;?>

We will use unset () to destroy both $ s and $ p. then we can see that the memory usage difference is also 272, which means the memory can be released. Then, we can draw another conclusion:
Conclusion 2: The memory is released only when all variables pointing to the variable (such as referenced variables) are destroyed.

The above is the most comprehensive solution for php memory overflow. For more information, see PHP Chinese network (www.php1.cn )!

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.