[PHP] substr佔用內存謹慎使用

在下面的場景中使用substr的時候, 有時候會報超出內存fatal error ,當curl讀取的內容過大的時候php

            $header_size = curl_getinfo($curl_handle, CURLINFO_HEADER_SIZE);
            $this->response_headers = substr($this->response, 0, $header_size);
            $this->response_body = substr($this->response, $header_size);
            $this->response_code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
            $this->response_info = curl_getinfo($curl_handle);

用substr切分curl返回的內容 , 分出header頭和body體curl

1. 測試一下:測試

<?php
$start=memory_get_usage();
$str="123";
var_dump(memory_get_usage()-$start);
//$a=substr($str,0,1);
$a=$str;
var_dump(memory_get_usage()-$start-24);
$b=$str;
//$b=mb_substr($str,1);
var_dump(memory_get_usage()-$start-24);
$c=$str;
//$b=mb_substr($str,1);
var_dump(memory_get_usage()-$start-24);

這個變量賦值,佔48字節this

2.使用substr切分後url

<?php
$start=memory_get_usage();
$str="123";
var_dump(memory_get_usage()-$start);
//$a=substr($str,0,1);
$a=$str;
var_dump(memory_get_usage()-$start-24);
//$b=$str;
$b=substr($str,0,1);
var_dump(memory_get_usage()-$start-24);
//$c=$str;
$c=substr($str,1);
var_dump(memory_get_usage()-$start-24);

切分後賦值,佔到了92字節 和 96字節 , 比原變量漲了一倍多code

相關文章
相關標籤/搜索