1. the memory overflow solution often encounters large arrays during data statistics and analysis, and may cause memory overflow. here is my solution. The following example illustrates the problem: if the number of records in the log is 500000, the solution is as follows: ini_set (& lsquo; memory_limit & rsquo;, & rsquo; 64 M & rsquo ;)
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:
Ini_set ('memory _ limit ', '64m'); set_time_limit (600); $ 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?
Farr )){???? Echo ?"
No related records!
";???? Exit;} while (! Empty ($ farr )){???? $ _ Sub? =? Array_splice ($ farr ,? 0 ,? 10000 );???? 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);} 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: destroying variables and releasing 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 destroying variables cannot be reached with unset! Let's first look at an example:
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:
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:
'; 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:
'; 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:
'; 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.