首页 > PHP > 一个 PHP 数组有多大?

一个 PHP 数组有多大?

2009年07月23号
查看评论 发表评论 295次浏览

PHP在数组处理上非常低效,尤其是内存占用较多,常常使得httpd进程消耗太多资源。虽然通常在PHP中进行大量数组运算从一定程度上反应程序设计上可能存在问题,但是粗略的估计数组占用的内存是很有必要的。

首先感觉一下1000个元素的整数数组占有的内存:

echo memory_get_usage() . “\n”;

$a = Array();
for ($i=0; $i<1000; $i++) {
    $a[$i] = $i + $i;
}

echo memory_get_usage() . “\n”;

for ($i=1000; $i<2000; $i++) {
    $a[$i] = $i + $i;
}

echo memory_get_usage() . “\n”;

输出是:

58176
162956
267088

大 约可以知道 1000 个元素的整数数组需要占用 100k 内存,平均每个元素占用 100 个字节。而纯 C 中整体只需要 4k。memory_get_usage() 返回的结果并不是全是被数组占用了,还要包括一些 PHP 运行本身分配的一些结构,可能用内置函数生成的数组更接近真实的空间:

echo “init mem: ” . memory_get_usage() . “\n”;
$a = array_fill(0, 10000, 1);
echo “10k elements: ” . memory_get_usage() . “, system: ” . memory_get_usage(true) . “\n”;
$b = array_fill(0, 10000, 1);
echo “10k elements: ” . memory_get_usage() . “, system: ” . memory_get_usage(true) . “\n”;

得到:

init mem: 58468
10k elements: 724696, system: 786432
10k elements: 1390464, system: 1572864

从这个结果来看似乎一个数组元素大约只占用了 60 个左右的字节。再看看数组的C结构,PHP 中的数组变量,首先需要一个 zval 结构:

struct _zval_struct {  
    zvalue_value value;
    zend_uint refcount__gc;
    zend_uchar type;
    zend_uchar is_ref__gc;
};

zvalue_value 是一个union:

typedef union _zvalue_value {
    long lval;
    double dval;
    struct {
        char *val;
        int len;
    } str;
    HashTable *ht;
    zend_object_value obj;
} zvalue_value;

通常 zval 结构需要 8+6=14 个字节,PHP中每个变量都有对应的 zval,但是数组,字符串和对象还需要另外的存储结构,而数组则是一个 HashTable :

typedef struct _hashtable {
    uint nTableSize;
    uint nTableMask;
    uint nNumOfElements;
    ulong nNextFreeElement;
    Bucket *pInternalPointer;
    Bucket *pListHead;
    Bucket *pListTail;
    Bucket **arBuckets;
    dtor_func_t pDestructor;
    zend_bool persistent;
    unsigned char nApplyCount;
    zend_bool bApplyProtection;
} HashTable;

HashTable 结构需要 40 个字节,每个数组元素存储在 Bucket 结构中:

typedef struct bucket {
    ulong h;
    uint nKeyLength;
    void *pData;
    void *pDataPtr;
    struct bucket *pListNext;
    struct bucket *pListLast;
    struct bucket *pNext;
    struct bucket *pLast;
    char arKey[1];
} Bucket;

Bucket 结构需要 36 个字节,键长超过四个字节的部分附加在 Bucket 后面,而元素值很可能是一个 zval 结构,另外每个数组会分配一个由 arBuckets 指向的 Bucket 指针数组, 虽然不能说每增加一个元素就需要一个指针,但是实际情况可能更糟。这么算来一个数组元素就会占用 54 个字节,与上面的估算相差不远。

一个空数组至少会占用 14(zval) + 40(HashTable) + 32(arBuckets) = 86 个字节,作为一个变量应该在符号表中有个位置,也是一个数组元素,因此一个空数组变量需要 118 个字节来描述和存储。从空间的角度来看,小型数组平均代价较大,当然一个脚本中不会充斥数量很大的小型数组,可以以较小的空间代价来获取编程上的快捷。
但如果将数组当作容器来使用就是另一番景象了,实际应用经常会遇到多维数组,而且元素居多。比如10k个元素的一维数组大概消耗540k内存,而10k x 10 的二维数组理论上只需要 6M 左右的空间,但是按照 memory_get_usage 的结果则两倍于此,[10k,5,2]的三维数组居然消耗了23M,小型数组果然是划不来的

类别PHP 标签
  1. 游客
    发表于 2009年07月29号 20时44分07秒 | 1楼

    平常要多加注意了,谢谢

  2. puma testastretta
    发表于 2010年06月19号 16时21分35秒 | 2楼

    you have an open mind

  3. Air Force 1 Shoes on Sale
    发表于 2010年06月24号 01时21分59秒 | 3楼

    gratitude makes the world go around… thanks for the story

  4. Joe Thornton Jersey
    发表于 2010年06月24号 05时33分30秒 | 4楼

    wow! Thank you for inspiring the heart of a writer within us.

  5. cheap basketball shoes
    发表于 2010年07月01号 11时18分21秒 | 5楼

    Well written!

  6. mbt anti shoe
    发表于 2010年07月09号 14时30分35秒 | 6楼

    That was so beautifully put. Definitely a new way of looking at life.

  7. paul smith shirt
    发表于 2010年07月20号 11时21分19秒 | 7楼

    This article fills me with wonder at the reverence for, well, Reminds me of something.

  8. Nike AF1
    发表于 2010年07月25号 14时06分04秒 | 8楼

    I was just scanning this posting and was reminded of what I once read.

  9. Lacoste Online
    发表于 2010年07月30号 11时51分07秒 | 9楼

    I was thinking today that being grateful every moment is great freedom.