PHP kernel (memory management 1) PHP memory management
Before 5.3, PHP used the reference counting method.
PHP adopted a new garbage collection mechanism after 5.3The operating system calls back when applying for memory space.
When the operating system applies for memory space, it will switch the CPU from the user state to the kernel state, the switching cost is very high (will cause performance problems)
PHP default maximum memory usage 32 M
In php. ini
memory_limit=32M
Or use the dynamic mode to modify the maximum memory:
Obtain current memory usage
Memory_get_usage () // Current memory size used by the PHP script memory_get_peak_usage () // return the memory usage peak at the current position, so that you can know the memory peak autoload () // avoid containing classes that may not be used at one time
PHP memory distribution and management
| Application-layer Applecation |
| ----- |
| Interface layer Emalloc/efree | Heap | storage layer Storange |
| Malloc | win32 | mmap_anon | mmap_zero |
Storage layer: use malloc and mmap to apply for real space from the system and release the space using the free function.
Control Layer: controls the entire memory management process (control layer is the core of PHP memory management)
Use macro definition to separate upper and lower layers (facade mode)Memory management
Initialization:
Zend_mm_startup (); // initialize the storage layer zend_mm_startup_ex (); // initialize the heap layer ZEND_MM_SEG_SIZE // Default 256 * 1024ZEND_MM_SEG_SIZE // Four default memory allocation schemes
PHP memory maintenance list
- Small memory table: free_buckets
- Large memory table: larg_free_buckets
Remaining memory table: rest_buckets
The first two are HashTable, and each HashTable has its own hash function.