|
for ( $i = 1; $i < 100; $i++ ) {
$str = str_repeat('01234567', $i);
$a = memory_get_usage();
unset($str);
$b = memory_get_usage();
echo "/n
".$i.': '.($b - $a).' Bytes.';
}
從結(jié)果看出:
8 x 32 = 256 在256字節(jié)長(zhǎng)的時(shí)候才真正有必要釋放內(nèi)存,有些人說(shuō),不如直接$str = null來(lái)的速度快。
結(jié)果如下:
1: 0 Bytes.
2: 0 Bytes.
3: 0 Bytes.
4: 0 Bytes.
5: 0 Bytes.
6: 0 Bytes.
7: 0 Bytes.
8: 0 Bytes.
9: 0 Bytes.
10: 0 Bytes.
11: 0 Bytes.
12: 0 Bytes.
13: 0 Bytes.
14: 0 Bytes.
15: 0 Bytes.
16: 0 Bytes.
17: 0 Bytes.
18: 0 Bytes.
19: 0 Bytes.
20: 0 Bytes.
21: 0 Bytes.
22: 0 Bytes.
23: 0 Bytes.
24: 0 Bytes.
25: 0 Bytes.
26: 0 Bytes.
27: 0 Bytes.
28: 0 Bytes.
29: 0 Bytes.
30: 0 Bytes.
31: 0 Bytes.
32: -272 Bytes.
33: -280 Bytes.
34: -288 Bytes.
35: -296 Bytes.
36: -304 Bytes.
37: -312 Bytes.
38: -320 Bytes.
39: -328 Bytes.
40: -336 Bytes.
41: -344 Bytes.
42: -352 Bytes.
43: -360 Bytes.
44: -368 Bytes.
45: -376 Bytes.
46: -384 Bytes.
47: -392 Bytes.
48: -400 Bytes.
49: -408 Bytes.
50: -416 Bytes.
51: -424 Bytes.
52: -432 Bytes.
53: -440 Bytes.
54: -448 Bytes.
55: -456 Bytes.
56: -464 Bytes.
57: -472 Bytes.
58: -480 Bytes.
59: -488 Bytes.
60: -496 Bytes.
61: -504 Bytes.
62: -512 Bytes.
63: -520 Bytes.
64: -528 Bytes.
65: -536 Bytes.
66: -544 Bytes.
67: -552 Bytes.
68: -560 Bytes.
69: -568 Bytes.
70: -576 Bytes.
71: -584 Bytes.
72: -592 Bytes.
73: -600 Bytes.
74: -608 Bytes.
75: -616 Bytes.
76: -624 Bytes.
77: -632 Bytes.
78: -640 Bytes.
79: -648 Bytes.
80: -656 Bytes.
81: -664 Bytes.
82: -672 Bytes.
83: -680 Bytes.
84: -688 Bytes.
85: -696 Bytes.
86: -704 Bytes.
87: -712 Bytes.
88: -720 Bytes.
89: -728 Bytes.
90: -736 Bytes.
91: -744 Bytes.
92: -752 Bytes.
93: -760 Bytes.
94: -768 Bytes.
95: -776 Bytes.
96: -784 Bytes.
97: -792 Bytes.
98: -800 Bytes.
99: -808 Bytes.
我們先看一個(gè)例子
復(fù)制代碼 代碼如下:
<?php
$s=str_repeat('1',255); //產(chǎn)生由255個(gè)1組成的字符串
$m=memory_get_usage(); //獲取當(dāng)前占用內(nèi)存
unset($s);
$mm=memory_get_usage(); //unset()后再查看當(dāng)前占用內(nèi)存
echo $m-$mm;
?>
最后輸出unset()之前占用內(nèi)存減去unset()之后占用內(nèi)存,如果是正數(shù),那么說(shuō)明unset($s)已經(jīng)將$s從內(nèi)存中銷毀(或者說(shuō),unset()之后內(nèi)存占用減少了),可是我在php5和windows平臺(tái)下,得到的結(jié)果是:-48。這是否可以說(shuō)明,unset($s)并沒(méi)有起到銷毀變量$s所占用內(nèi)存的作用呢?我們?cè)僮飨旅娴睦樱?
復(fù)制代碼 代碼如下:
<?php
$s=str_repeat('1',256); //產(chǎn)生由256個(gè)1組成的字符串
$m=memory_get_usage(); //獲取當(dāng)前占用內(nèi)存
unset($s);
$mm=memory_get_usage(); //unset()后再查看當(dāng)前占用內(nèi)存
echo $m-$mm;
?>
這個(gè)例子,和上面的例子幾乎相同,唯一的不同是,$s由256個(gè)1組成,即比第一個(gè)例子多了一個(gè)1,得到結(jié)果是:224。這是否可以說(shuō)明,unset($s)已經(jīng)將$s所占用的內(nèi)存銷毀了?
通過(guò)上面兩個(gè)例子,我們可以得出以下結(jié)論:結(jié)論一、unset()函數(shù)只能在變量值占用內(nèi)存空間超過(guò)256字節(jié)時(shí)才會(huì)釋放內(nèi)存空間。
那么是不是只要變量值超過(guò)256,使用unset就可以釋放內(nèi)存空間呢?我們?cè)偻ㄟ^(guò)一個(gè)例子來(lái)測(cè)試一下:
復(fù)制代碼 代碼如下:
<?php
$s=str_repeat('1',256); //這和第二個(gè)例子完全相同
$p=&$s;
$m=memory_get_usage();
unset($s); //銷毀$s
$mm=memory_get_usage();
echo $p.'<br />';
echo $m-$mm;
?>
'刷新頁(yè)面,我們看到第一行有256個(gè)1,第二行是-48,按理說(shuō)我們已經(jīng)銷毀了$s,而$p只是引用$s的變量,應(yīng)該是沒(méi)有內(nèi)容了,另外,unset($s)后內(nèi)存占用卻比unset()前增加了!現(xiàn)在我們?cè)僮鲆韵碌睦樱?
復(fù)制代碼 代碼如下:
<?php
$s=str_repeat('1',256); //這和第二個(gè)例子完全相同
$p=&$s;
$m=memory_get_usage();
$s=null; //設(shè)置$s為null
$mm=memory_get_usage();
echo $p.'<br />';
echo $m-$mm;
?>
現(xiàn)在刷新頁(yè)面,我們看到,輸出$p已經(jīng)是沒(méi)有內(nèi)容了,unset()前后內(nèi)存占用量之差是224,即已經(jīng)清除了變量占用的內(nèi)存。本例中的$s=null也可以換成unset(),如下:
復(fù)制代碼 代碼如下:
<?php
$s=str_repeat('1',256); //這和第二個(gè)例子完全相同
$p=&$s;
$m=memory_get_usage();
unset($s); //銷毀$s
unset($p);
$mm=memory_get_usage();
echo $p.'<br />';
echo $m-$mm;
?>
我們將$s和$p都使用unset()銷毀,這時(shí)再看內(nèi)存占用量之差也是224,說(shuō)明這樣也可以釋放內(nèi)存。那么,我們可以得到另外一條結(jié)論:結(jié)論二、只有當(dāng)指向該變量的所有變量(如引用變量)都被銷毀后,才會(huì)釋放內(nèi)存。
相信經(jīng)過(guò)本文的例子后,大家應(yīng)該對(duì)unset()有所了解了,最起碼,本人用unset()也是為了在變量不起作用時(shí),釋放內(nèi)存。
php技術(shù):PHP中使用unset銷毀變量并內(nèi)存釋放問(wèn)題,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。