How to solve PHP memory overflow problem?

coldplay.xixi
Release: 2023-03-01 20:12:01
Original
4858 people have browsed it

Methods to solve PHP memory overflow problem: 1. Increase the available memory size of PHP; 2. Process arrays in batches and destroy used variables in time; 3. Reduce the use of static variables as much as possible; 4. , After the database operation is completed, the connection must be closed immediately.

How to solve PHP memory overflow problem?

Methods to solve PHP memory overflow problem:

1. Memory overflow solution

When doing statistical analysis of data, we often encounter large arrays, and memory overflow may occur. Here I will share my solution. Let’s use an example to illustrate this problem, as follows:

Assume that the number of records stored in the log is 500,000, then the solution is as follows:

ini_set(‘memory_limit’,’64M’); 
Copy after login

//Reset the memory size that can be used by PHP to 64M. Generally, the php.ini file cannot be modified on the remote host and can only be set through the program.

Note: Insafe_mode(safe mode),ini_setis invalid.

set_time_limit(600);//设置超时限制为6分钟 $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);////将文件读入数组中 array_shift($arr);//移出第一个单元-》 $farr = array_merge($farr,$arr); unset($arr); } } if(empty($this->farr)){ echo ”

没有相关记录!

”; exit; } while(!empty($farr)){ $_sub = array_splice($farr, 0, 10000); //每次取出$farr中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);//用完及时销毁 } unset($farr);
Copy after login

Here, it is not difficult to see:

1. On the one hand, we need to increase the available memory size of PHP. On the other hand, as long as we find a way to process the array in batches, divide and conquer, we will Used variables are destroyed in time (unset), and generally there will be no overflow problems.

2. In addition, in order to save PHP program memory consumption, we should reduce the use of static variables as much as possible. When data reuse is needed, you can consider using references (&).

3. Another point is: after the database operation is completed, the connection must be closed immediately; after an object is used, the destructor (__destruct()) must be called promptly.

two. Unset destroys variables and releases memory issues

PHP’sunset()function is used to clear and destroy variables. For unused variables, we can useunset()Destroy it. But sometimes, using unset() cannot destroy the memory occupied by the variable! Let’s look at an example first:

Copy after login

Final outputunset()The memory occupied before minus the memory occupied afterunset()If it is a positive number, then it meansunset($s)$s has been destroyed from the memory (or in other words, the memory usage has been reduced after unset()), but under PHP5 and windows platforms, the result I got is: 0. Does this mean thatunset($s)does not destroy the memory occupied by variable $s? Let’s make the following example again:

Copy after login

This example is almost the same as the above example. The only difference is that$sis composed of 256 1’s, which is more than the first example. After adding a 1, the result is: 272. Does this mean thatunset($s)has destroyed the memory occupied by$s?

Through the above example, we can draw the following conclusions:

Conclusion 1.unset()The function can only be used when the variable value occupies more than 256 bytes of memory space. Memory space will be released.

Conclusion 2. The memory will be released only when all variables (such as reference variables) pointing to the variable are destroyed.

Related learning recommendations:PHP programming from entry to proficiency

The above is the detailed content of How to solve PHP memory overflow problem?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!