PHP Memory Overflow Solution

Source: Internet
Author: User

A Memory Overflow Solution

When doing statistical analysis of the data, often encounter large arrays, memory overflow may occur, here to share my solution. Or an example to illustrate the problem, as follows:

Assuming that the number of records stored in the log is 500000 , the solution is as follows:

Ini_set (' Memory_limit ', ' 64M '); Reset php can use a memory size of 64M, generally on the remote host is not able to modify the php.ini file, only through the program settings. Note: under Safe_mode (Safe mode), theini_set fails

Set_time_limit (+//) set timeout limit to 6 minutes

$farr = $Uarr = $Marr = $IParr = $data = $_sub = Array ();

$SPT = "[Email protected]#!$";

$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);////reads the files into the array

Array_shift ($arr);//Move out of the first unit-"<?php exit;? >

$farr = Array_merge ($farr, $arr);

Unset ($arr);

}

}

if (Empty ($this->farr)) {

echo "<p><center> no Related records! </center></p> ";

Exit

}

while (!empty ($farr)) {

$_sub = Array_splice ($farr, 0, 10000); 1000 $farr in each removal

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);//Run out of time to destroy

}

Unset ($farr);

Here, it is not difficult to see, on the one hand, we want to increase the size of the available PHP memory, on the other hand, as long as we find ways to batch processing, divide and conquer, will be used to destroy the variables in time (unset), generally will not overflow problem.

In addition, in order to save PHP Program memory loss, we should minimize the use of static variables, when the need for data reuse, you can consider using references (&). One more thing: Once the database operation is complete, close the connection immediately; When an object is exhausted, call the destructor (__destruct ()) in a timely manner.

two. unset destroying variables and freeing memory issues

The PHP unset () function is used to clear, destroy variables, and we can destroy them with unset (). But at some point, using unset () does not achieve the memory that is consumed by destroying variables! Let's look at an example first:

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

After the last output unset () takes up memory minus unset () before it takes up memory, if it is a positive number, then unset ($s) has destroyed $s from memory (or, after unset () the memory footprint is reduced), but I am under PHP5 and Windows platform, The resulting result is: 0. Does this mean that unset ($s) does not play the role of destroying the memory that the variable $s consumes? Let us make the following example:

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

This example is almost identical to the example above, the only difference being that the $s consists of 256 1, which is a 1 more than the first example, and the result is: 272. Does this indicate that unset ($s) has destroyed the memory occupied by $s?
From the above two examples, we can draw the following conclusions:
Conclusion the unset () function frees up memory space only if the value of the variable takes up more than 256 bytes of memory.

So is it possible to use unset to free up memory space as long as the value of the variable exceeds 256? Let's test it with one more example:

<?php
$s=str_repeat(‘1‘,256); //这和第二个例子完全相同
$p=&$s;
$m=memory_get_usage();
unset($s); //销毁$s
$mm=memory_get_usage();
echo $p.‘<br />‘;
echo $m-$mm;
?>

Refresh the page, we see the first line has 256 1, the second row is 0, supposedly we have destroyed $s, and $p just reference $s variables, should be no content, in addition, unset ($s) before and after the memory consumption has not changed! Now let's do the following example:

<?php
$s=str_repeat(‘1‘,256); //这和第二个例子完全相同
$p=&$s;
$m=memory_get_usage();
$s=null; //设置$s为null
$mm=memory_get_usage();
echo $p.‘<br />‘;
echo $m-$mm;
?>

Now refresh the page, we see that the output $p has no content, unset () before and after the memory consumption of the difference is 272, that has cleared the memory of the variable occupied. The $s=null in this example can also be replaced with unset (), as follows:

<?php
$s=str_repeat(‘1‘,256); //这和第二个例子完全相同
$p=&$s;
$m=memory_get_usage();
unset($s); //销毁$s
unset($p);
$mm=memory_get_usage();
echo $p.‘<br />‘;
echo $m-$mm;
?>

We destroy both $s and $p using unset (), and then the difference between memory consumption is 272, which means that memory can also be freed. So, we can get another conclusion:
Conclusion the memory is freed only when all variables (such as reference variables) that point to the variable are destroyed.

PHP Memory Overflow Solution

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.