search
HomeBackend DevelopmentPHP TutorialDetailed analysis of low memory utilization of PHP array

Does PHP really consume so much memory? So I took this opportunity to learn about PHP's data type implementation. This article mainly introduces the detailed interpretation of low memory utilization and weak types of PHP arrays. It has certain reference value and interested friends can refer to it.

Let’s do a test first:


<?php 
  echo memory_get_usage() , &#39;<br>&#39;; 
  $start = memory_get_usage(); 
  $a = Array(); 
  for ($i=0; $i<1000; $i++) { 
   $a[$i] = $i + $i; 
  } 
  $end = memory_get_usage(); 
  echo memory_get_usage() , &#39;<br>&#39;; 
  echo &#39;argv:&#39;, ($end - $start)/1000 ,&#39;bytes&#39; , &#39;<br>&#39;;

The result obtained:

353352
437848
argv: 84.416bytes

An integer array of 1000 elements consumes (437848 - 353352) bytes of memory, which is approximately 82KB, which means that each element occupies 84 bytes of memory. In C language, an int occupies 4 bytes, which is a 20-fold difference overall.

But it is said on the Internet that the results returned by memory_get_usage() are not all occupied by arrays, but also include some structures of PHP itself. Therefore, try another way and use PHP built-in functions to generate arrays:


<?php 
  $start = memory_get_usage(); 
  $a = array_fill(0, 10000, 1); 
  $end = memory_get_usage(); //10k elements array; 
  echo &#39;argv:&#39;, ($end - $start )/10000,&#39;byte&#39; , &#39;<br>&#39;;

The output is:

argv:54.5792byte

is slightly better than just now, but it is still 54 bytes, which is indeed 10 worse. About times.

The reason has to start with the underlying implementation of PHP. PHP is a weakly typed language, regardless of int, double, string, etc., a unified '$' can solve all problems. The bottom layer of PHP is implemented in C language. Each variable corresponds to a zval structure, which is defined in detail as:


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 uses the union structure to store the value of the variable, zvalue_value in zval The value variable of the type is a union, defined as follows:


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;

The size of the memory occupied by the union type is determined by the data space occupied by its largest member. In zvalue_value, the int of the str structure occupies 4 bytes and the char pointer occupies 4 bytes, so the memory occupied by the entire zvalue_value is 8 bytes.

The size of zval is 8 + 4 + 1 + 1 = 14 bytes.

Notice that there is also a HashTable in zvalue_value. What does it do? In zval, arrays, strings and objects also require additional storage structures. The storage structure of arrays is HashTable.

The definition of HashTable is given:


typedef struct _hashtable { 
   uint nTableSize; //表长度,并非元素个数 
   uint nTableMask;//表的掩码,始终等于nTableSize-1 
   uint nNumOfElements;//存储的元素个数 
   ulong nNextFreeElement;//指向下一个空的元素位置 
   Bucket *pInternalPointer;//foreach循环时,用来记录当前遍历到的元素位置 
   Bucket *pListHead; 
   Bucket *pListTail; 
   Bucket **arBuckets;//存储的元素数组 
   dtor_func_t pDestructor;//析构函数 
   zend_bool persistent;//是否持久保存。从这可以发现,PHP数组是可以实现持久保存在内存中的,而无需每次请求都重新加载。 
   unsigned char nApplyCount; 
   zend_bool bApplyProtection; 
} HashTable;

In addition to several attribute variables that record the size of the table and the number of elements it contains, Bucket has been used multiple times When used, how Bucket is defined:


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]; /* Must be last element */ 
} Bucket;

is a bit like a linked list. Bucket is like a linked list node with specific data and pointers, and HashTable is an array. , holds a string of Bucket elements. The implementation of multi-dimensional arrays in PHP is just another HashTable stored in the Bucket.

Calculate that HashTable takes up 39 bytes and Bucket takes up 33 bytes. An empty array takes up 14 + 39 + 33 = 86 bytes. The Bucket structure requires 33 bytes, and the part with a key length longer than four bytes is appended to the Bucket, and the element value is likely to be a zval structure. In addition, each array will be allocated a Bucket pointer array pointed to by arBuckets, although it cannot be said Each additional element requires a pointer, but the situation could be worse. This calculates that one array element will occupy 54 bytes, which is almost the same as the above estimate.

From a space perspective, the average cost of small arrays is larger. Of course, a script will not be filled with a large number of small arrays, and you can gain programming convenience at a smaller space cost. . But if you use an array as a container, it's a different story. In practical applications, you often encounter multi-dimensional arrays with many elements. For example, a one-dimensional array of 10k elements consumes approximately 540k of memory, while a two-dimensional array of 10k The array actually consumes 23M. Small arrays are really not worth it.

The reason for the low memory utilization of PHP arrays has been mentioned here. The following article will explain the specific implementation of PHP array operations.

Related recommendations:

Solution to the problem that PHP array memory consumes too much_PHP tutorial

Solve the problem that PHP array consumes too much memory

Solution to the problem that PHP array consumes too much memory_php tips

The above is the detailed content of Detailed analysis of low memory utilization of PHP array. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!