Home >Backend Development >PHP Problem >How to solve PHP memory overflow problem?

How to solve PHP memory overflow problem?

coldplay.xixi
coldplay.xixiOriginal
2020-07-01 16:27:455011browse

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’); 

//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: In safe_mode (safe mode), ini_set is 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);//移出第一个单元-》<?php exit;?>
$farr = array_merge($farr,$arr);
unset($arr);
}
}
if(empty($this->farr)){
echo ”<p><center>没有相关记录!</center></p>”;
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);

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’s unset() function is used to clear and destroy variables. For unused variables, we can use unset()Destroy it. But sometimes, using unset() cannot destroy the memory occupied by the variable! Let’s look at an example first:

<?php
$s=str_repeat(&#39;1&#39;,255); //产生由255个1组成的字符串
$m=memory_get_usage(); //获取当前占用内存
unset($s);
$mm=memory_get_usage(); //unset()后再查看当前占用内存
echo $m-$mm;
?>

Final outputunset()The memory occupied before minus the memory occupied after unset()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 that unset($s) does not destroy the memory occupied by variable $s? Let’s make the following example again:

<?php
$s=str_repeat(&#39;1&#39;,256); //产生由256个1组成的字符串
$m=memory_get_usage(); //获取当前占用内存
unset($s);
$mm=memory_get_usage(); //unset()后再查看当前占用内存
echo $m-$mm;
?>

This example is almost the same as the above example. The only difference is that $s is composed of 256 1’s, which is more than the first example. After adding a 1, the result is: 272. Does this mean that unset($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!

Statement:
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