1. Why memory management is required
Because the computer's memory is managed by the operating system, the normal application is unable to access the memory directly, and the application can only request memory from the operating system, and the usual application does so, applying memory to the operating system through library functions like malloc, when needed. In some performance-demanding scenarios where frequent use and release of memory is required, such as Web servers, programming languages, etc., due to the system calls to the operating system for memory space, system calls and normal application-layer function calls perform very differently because system calls switch the CPU from the user state to the kernel , because it involves the operation of physical memory, only the operating system can be carried out, and the cost of such a switchover is very large, if the frequent switching between the kernel state and the user state will produce performance problems.
Some performance-demanding applications typically do their own memory management in the user state, such as the first request for a slightly larger amount of memory to reserve, while the use of freed memory is not immediately returned to the operating system, memory can be reused, which can avoid multiple memory requests and the performance of the release of the consumption.
2, the memory management of PHP
PHP's memory management is divided into three tiers: the storage layer (storage), the heap layer (heap), and the interface layer (Emalloc/efree).
The storage layer actually applies memory to the system through functions such as malloc (), mmap (), and frees the requested memory through the free () function. The storage layer usually applies a large amount of memory, the application of large memory is not to refer to the storage layer structure requires large memory, but the heap layer by calling the storage layer allocation method, its large chunks of memory, the role of the storage layer is to allocate memory to the heap layer transparency. , PHP has 4 memory allocation schemes in the storage tier: Malloc,win32,mmap_anon,mmap_zero, which uses malloc to allocate memory by default, or Windows version if ZEND_WIN32 macros are set. Call HeapAlloc to allocate memory, the remaining two memory schemes are anonymous memory mappings, and the PHP memory scheme can be modified by setting environment variables.
- Interface Layer
Interface Layer macro definition (location: zend/zend_alloc.h,php5.6.10)
/* Standard wrapper macros */#define EMALLOC (size) _emalloc ((size) zend_file_line_cc zend_file_line_empty_cc) #define Safe_emalloc (nmemb, size, offset) _safe_emalloc ((NMEMB), (size), (offset) zend_file_line_cc zend_file_line_empty_cc) # Define Efree (PTR) _efree ((PTR) zend_file_line_cc zend_file_line_empty_cc) #define ECALLOC (nmemb, size) _ecalloc ((nmemb ), (size) zend_file_line_cc zend_file_line_empty_cc) #define EREALLOC (PTR, size) _erealloc ((PTR), (size), 0 zend_file_ LINE_CC zend_file_line_empty_cc) #define SAFE_EREALLOC (PTR, nmemb, size, offset) _safe_erealloc ((PTR), (NMEMB), (size), (offset) ZEND_FILE_LINE_CC zend_file_line_empty_cc) #define Erealloc_recoverable (PTR, size) _erealloc ((PTR), (size), 1 zend_ FILE_LINE_CC zend_file_line_empty_cc) #define ESTRDUP (s) _estrdup ((s) zend_file_line_cc zend_file_line_empty_cc) # Define ESTRNDUP (S, length) _estrndup ((s), (length) zend_file_line_cc zend_file_line_empty_cc) #define Zend_mem_block_ Size (PTR) _zend_mem_block_size (PTR) tsrmls_cc zend_file_line_cc Zend_fiLE_LINE_EMPTY_CC)/* Relay wrapper macros */#define EMALLOC_REL (size) _emalloc ((size) zend_file_line_relay_cc Zend_file _LINE_CC) #define SAFE_EMALLOC_REL (NMEMB, size, offset) _safe_emalloc ((NMEMB), (size), (offset) Zend_file_line_relay_ CC zend_file_line_cc) #define Efree_rel (PTR) _efree ((PTR) zend_file_line_relay_cc zend_file_line_cc) #define ECALLOC_ Rel (nmemb, size) _ecalloc ((NMEMB), (size) zend_file_line_relay_cc zend_file_line_cc) #define Erealloc_rel (PTR, size) _ Erealloc (PTR), (size), 0 zend_file_line_relay_cc zend_file_line_cc) #define Erealloc_recoverable_rel (PTR, size) _ Erealloc (PTR), (size), 1 zend_file_line_relay_cc zend_file_line_cc) #define Safe_erealloc_rel (PTR, nmemb, size, offset ) _safe_erealloc (PTR), (NMEMB), (size), (offset) zend_file_line_relay_cc zend_file_line_cc) #define Estrdup_rel (s) _ Estrdup ((s) zend_file_line_relay_cc zend_file_line_cc) #define ESTRNDUP_REL (s, length) _estrndup ((s), (length) Zend_ FILE_LINE_RELAY_CC zend_file_line_cc) #define Zend_mem_block_size_rel (PTR) _zend_meM_block_size (PTR) tsrmls_cc zend_file_line_relay_cc zend_file_line_cc)
PHP Memory Management