PHP數組占用內存分析
下面的做法會占用多大的內存?
list($appid,$openid) = ["testcontent","test"];
測試
$m0 = memory_get_usage();
$k = range(1,200000);
$m1 = memory_get_usage();
echo round(($m1-$m0)/pow(1024,2),4) ."MB\n";
foreach ($k as $i){
$n1 = "kk$i";
$n2 = "tt$i";
list($$n1,$$n2) = [$i,$i*3];
}
$m2 = memory_get_usage();
echo round(($m2-$m1)/pow(1024,2),4) ."MB\n";
$m1 = memory_get_usage();
foreach ($k as $i){
$n1 = "kk$i";
$n2 = "tt$i";
$$n1 = $i+time();
$$n2 = 2*time();
}
$m2 = memory_get_usage();
echo round(($m2-$m1)/pow(1024,2),4) ."MB\n";
上面運行輸出的結果如下:
27.9404MB
51.3041MB
9.1553MB
可見數組占用的內存遠大于正常分配的內容
原理
在PHP中都使用long類型來代表數字,沒有使用int類型。大家都明白PHP是一種弱類型的語言,它不會去區分變量的類型,沒有int float char *之類的概念。我們看看php在zend里面存儲的變量,PHP中每個變量都有對應的 zval,Zval結構體定義在Zend/zend.h里面,其結構:
typedef struct _zval_struct zval;
struct _zval_struct {
/* Variable information */
zvalue_value value; /* The value 1 12字節(32位機是12,64位機需要8+4+4=16) */
zend_uint refcount__gc; /* The number of references to this value (for GC) 4字節 */
zend_uchar type; /* The active type 1字節*/
zend_uchar is_ref__gc; /* Whether this value is a reference (&) 1字節*/
};
PHP使用一種UNION結構來存儲變量的值,即zvalue_value 是一個union,UNION變量所占用的內存是由最大成員數據空間決定。
typedef union _zvalue_value {
long lval; /* long value */
double dval; /* double value */
struct { /* string value */
char *val;
int len;
} str;
HashTable *ht; /* hash table value */
zend_object_value obj; /*object value */
} zvalue_value;
最大成員數據空間是struct str,指針占*val用4字節,INT占用4字節,共8字節。struct zval占用的空間為8+4+1+1 = 14字節,其實呢,在zval中數組,字符串和對象還需要另外的存儲結構,數組則是一個 HashTable:
HashTable結構體定義在Zend/zend_hash.h.
typedef struct _hashtable {
uint nTableSize;//4
uint nTableMask;//4
uint nNumOfElements;//4
ulong nNextFreeElement;//4
Bucket *pInternalPointer; /* Used for element traversal 4*/
Bucket *pListHead;//4
Bucket *pListTail;//4
Bucket **arBuckets;//4
dtor_func_t pDestructor;//4
zend_bool persistent;//1
unsigned char nApplyCount;//1
zend_bool bApplyProtection;//1
#if ZEND_DEBUG
int inconsistent;//4
#endif
} HashTable;
HashTable 結構需要 39 個字節,每個數組元素存儲在 Bucket 結構中:
typedef struct bucket {
ulong h; /* Used for numeric indexing 4字節 */
uint nKeyLength; /* The length of the key (for string keys) 4字節 */
void *pData; /* 4字節*/
void *pDataPtr; /* 4字節*/
struct bucket *pListNext; /* PHP arrays are ordered. This gives the next element in that order4字節*/
struct bucket *pListLast; /* and this gives the previous element 4字節 */
struct bucket *pNext; /* The next element in this (doubly) linked list 4字節*/
struct bucket *pLast; /* The previous element in this (doubly) linked list 4字節*/
char arKey[1]; /* Must be last element 1字節*/
} Bucket;
Bucket 結構需要 33 個字節,鍵長超過四個字節的部分附加在 Bucket 后面,而元素值很可能是一個 zval 結構,另外每個數組會分配一個由 arBuckets 指向的 Bucket 指針數組, 雖然不能說每增加一個元素就需要一個指針,但是實際情況可能更糟。這么算來一個數組元素就會占用 54 個字節,與上面的估算幾乎一樣。
一個空數組至少會占用 14(zval) + 39(HashTable) + 33(arBuckets) = 86 個字節,作為一個變量應該在符號表中有個位置,也是一個數組元素,因此一個空數組變量需要 118 個字節來描述和存儲。從空間的角度來看,小型數組平均代價較大,當然一個腳本中不會充斥數量很大的小型數組,可以以較小的空間代價來獲取編程上的快捷。但如果將數組當作容器來使用就是另一番景象了,實際應用經常會遇到多維數組,而且元素居多。比如10k個元素的一維數組大概消耗540k內存,而10k x 10 的二維數組理論上只需要 6M 左右的空間,但是按照 memory_get_usage 的結果則兩倍于此,[10k,5,2]的三維數組居然消耗了23M,小型數組果然是劃不來的。
參考