Detailed memory: The storage layer in the PHP memory pool

Source: Internet
Author: User
Tags compact functions variables win32

The memory manager for PHP is layered (hierarchical). This manager has a total of three layers: storage layer (storage), heap (heap) layer and Emalloc/efree layer. The storage layer uses functions such as malloc (), mmap () to actually request memory from the system and release the requested memory through the free () function.
Storage tiers typically request large chunks of memory, here the application of large memory does not mean that the storage layer structure requires a large amount of memory, but the heap layer by calling the storage layer allocation method, the format of the paragraph to request the memory is relatively large, the role of the storage layer is to allocate memory to the heap layer transparent.
First look at the structure of the storage layer:
/* Heaps with User defined storage * *
typedef struct _ZEND_MM_STORAGE zend_mm_storage;

typedef struct _ZEND_MM_SEGMENT {
size_t size;
struct _zend_mm_segment *next_segment;
} zend_mm_segment;

typedef struct _ZEND_MM_MEM_HANDLERS {
const char *name;
Zend_mm_storage* (*init) (void *params); initialization function
void (*dtor) (Zend_mm_storage *storage); destructor
void (*compact) (Zend_mm_storage *storage);
zend_mm_segment* (*_alloc) (Zend_mm_storage *storage, size_t size); Memory allocation function
zend_mm_segment* (*_realloc) (Zend_mm_storage *storage, zend_mm_segment *ptr, size_t size); Reassigning memory functions
void (*_free) (Zend_mm_storage *storage, zend_mm_segment *ptr); Freeing memory functions
} zend_mm_mem_handlers;

struct _zend_mm_storage {
Const Zend_mm_mem_handlers *handlers; Processing a set of functions
void *data;
};
How memory is allocated, the function that is called is the set of processing functions in the _zend_mm_storage structure, and memory is expressed in terms of paragraph.
4 Kinds of memory schemes
PHP has 4 kinds of memory allocation schemes at the storage layer: Malloc,win32,mmap_anon,mmap_zero. The default is to allocate memory using malloc, if the ZEND_WIN32 macro is set, the Windows version, the HeapAlloc allocates memory, the remaining two memory schemes are anonymous memory mappings, and the PHP memory scheme can be modified by setting variables.
The official notes are as follows:
The Zend MM can be tweaked using zend_mm_mem_type and zend_mm_seg_size environment variables. Default values are "malloc" and "256K". Dependent on target system can be also use "Mmap_anon", "Mmap_zero" and "win32″storage managers.
In the code, for these 4 kinds of memory allocation scheme, respectively, corresponding to implement each processing function in Zend_mm_mem_handlers. A brief description of the code is as follows:
/* Use the MMAP memory-mapped function to allocate the private mapping of the copy when memory is written, and the mapping area is not associated with any files. */
# define ZEND_MM_MEM_MMAP_ANON_DSC {"Mmap_anon", Zend_mm_mem_dummy_init, Zend_mm_mem_dummy_dtor, Zend_mm_mem_dummy_ Compact, Zend_mm_mem_mmap_anon_alloc, Zend_mm_mem_mmap_realloc, zend_mm_mem_mmap_free} This article links http://www.cxybl.com/ Html/wlbc/php/20130922/40142.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.