一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - PHP中使用unset銷毀變量并內存釋放問題

PHP中使用unset銷毀變量并內存釋放問題

2020-01-04 19:20PHP教程網 PHP教程

PHP的unset()函數用來清除、銷毀變量,不用的變量,我們可以用unset()將它銷毀。但是某些時候,用unset()卻無法達到銷毀變量占用的內存!

代碼如下:


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.'; 


從結果看出: 
8 x 32 = 256 在256字節長的時候才真正有必要釋放內存,有些人說,不如直接$str = null來的速度快。 
結果如下: 
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. 

我們先看一個例子 

復制代碼代碼如下:


<?php 
$s=str_repeat('1',255); //產生由255個1組成的字符串 
$m=memory_get_usage(); //獲取當前占用內存 
unset($s); 
$mm=memory_get_usage(); //unset()后再查看當前占用內存 
echo $m-$mm; 
?> 


最后輸出unset()之前占用內存減去unset()之后占用內存,如果是正數,那么說明unset($s)已經將$s從內存中銷毀(或者說,unset()之后內存占用減少了),可是我在PHP5和windows平臺下,得到的結果是:-48。這是否可以說明,unset($s)并沒有起到銷毀變量$s所占用內存的作用呢?我們再作下面的例子: 

復制代碼代碼如下:


<?php 
$s=str_repeat('1',256); //產生由256個1組成的字符串 
$m=memory_get_usage(); //獲取當前占用內存 
unset($s); 
$mm=memory_get_usage(); //unset()后再查看當前占用內存 
echo $m-$mm; 
?> 


這個例子,和上面的例子幾乎相同,唯一的不同是,$s由256個1組成,即比第一個例子多了一個1,得到結果是:224。這是否可以說明,unset($s)已經將$s所占用的內存銷毀了? 
通過上面兩個例子,我們可以得出以下結論:結論一、unset()函數只能在變量值占用內存空間超過256字節時才會釋放內存空間。 
那么是不是只要變量值超過256,使用unset就可以釋放內存空間呢?我們再通過一個例子來測試一下: 

復制代碼代碼如下:


<?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; 
?> 


'刷新頁面,我們看到第一行有256個1,第二行是-48,按理說我們已經銷毀了$s,而$p只是引用$s的變量,應該是沒有內容了,另外,unset($s)后內存占用卻比unset()前增加了!現在我們再做以下的例子: 

復制代碼代碼如下:


<?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; 
?> 


現在刷新頁面,我們看到,輸出$p已經是沒有內容了,unset()前后內存占用量之差是224,即已經清除了變量占用的內存。本例中的$s=null也可以換成unset(),如下: 

復制代碼代碼如下:


<?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; 
?> 


我們將$s和$p都使用unset()銷毀,這時再看內存占用量之差也是224,說明這樣也可以釋放內存。那么,我們可以得到另外一條結論:結論二、只有當指向該變量的所有變量(如引用變量)都被銷毀后,才會釋放內存。 
相信經過本文的例子后,大家應該對unset()有所了解了,最起碼,本人用unset()也是為了在變量不起作用時,釋放內存。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 大肚孕妇的高h辣文 | 成人亚洲欧美日韩在线观看 | 国产理论片在线观看 | fulao在线观看的 | 午夜国产精品福利在线观看 | 日本草草视频 | 亚洲国产五月综合网 | 国产成人精品免费视频大全五级 | 99久久精品6在线播放 | 我的妹妹最近有点怪在线观看 | 日本精品中文字幕在线播放 | 日韩精品欧美激情国产一区 | yy8090韩国日本三理论免费 | 国产精品久久久久jk制服 | 亚洲国产在线视频中文字 | 午夜私人影院在线观看 | 青青91 | 久久草福利自拍视频在线观看 | ange venus与黑人 | 四虎在线精品观看免费 | 亚洲人成网站在线观看90影院 | 久久视热频国产这里只有精品23 | 四虎播放器 | 国产yw193.㎝m在线观看 | 国产成年人视频 | 亚洲一区二区日韩欧美gif | 大团圆6全文在线阅读 | 亚洲毛片基地4455ww | 国产成人久久 | 接吻吃胸摸下面啪啪教程 | 艹出白浆 | 日韩欧美在线观看综合网另类 | 91精品国产综合久久精品 | 亚洲伦理天堂 | 青草热视频 | 日韩欧美国产综合精品 | 亚洲日本va午夜中文字幕 | 国产99热 | sao虎影院桃红视频在线观看 | 成人影院免费在线观看 | 国产精品一区二区久久不卡 |