php 記憶體使用量資訊代碼

來源:互聯網
上載者:User

通過偵測指令碼的記憶體使用量情況,有利於代碼的最佳化。PHP 提供了一個垃圾收集器和一個非常複雜的記憶體管理器。指令碼執行時所使用的記憶體量,有升有跌。為了得到當前的記憶體使用量情況,我們可以使用 memory_get_usage() 函數。如果需要獲得任意時間點的最高記憶體使用量量,則可以使用 memory_limit() 函數。view sourceprint?01 echo "Initial:

 代碼如下 複製代碼

".memory_get_usage()." bytes "; 

02 /* prints 

03 Initial: 361400 bytes 

04 */

05   

06 // let's use up some memory 

07 for ($i = 0; $i < 100000; $i++) { 

08  $array []= md5($i); 

09 } 

10   

11 // let's remove half of the array 

12 for ($i = 0; $i < 100000; $i++) { 

13  unset($array[$i]); 

14 } 

15   

16 echo "Final: ".memory_get_usage()." bytes "; 

17 /* prints 

18 Final: 885912 bytes 

19 */

20   

21 echo "Peak: ".memory_get_peak_usage()." bytes "; 

22 /* prints 

23 Peak: 13687072 bytes 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.