Home > Article > Backend Development > Several areas of php memory
Divided into several areas of memory
1, Stack area (stack) - determined by the compiler when the program is running Automatic allocation, storing function parameter values, local variable values, etc. Its operation is similar to the stack in the data structure. It is automatically released by the compiler when the program ends. (Recommended learning: PHP video tutorial)
2. Heap area (heap) - open up another storage area in the memory. Generally allocated and released by the programmer. If the programmer does not release it, it may be recycled by the OS when the program ends. Note that it is different from the heap in the data structure. The allocation method is similar to a linked list, haha. Using malloc, calloc, realloc and other functions to allocate memory, what is allocated is
3, global area (static area) (static) on the heap - the compiler allocates memory when compiling. Global variables and static variables are stored together. For C language, initialized global variables and static variables are in one area, and uninitialized global variables and uninitialized static variables are in another adjacent area. But C does not have this difference - it is released by the system after the program ends
4, Literal constant area - the constant string is placed here. Released by the system after the program ends
5, Program code area - stores the binary code of the function body.
When programming in C language, developers must manually manage memory. Because PHP is often used as a module for web servers, memory management is closely related to preventing memory leaks.
Also be aware that PHP may be used in a threaded environment, which means that global variables may cause race conditions. For information about global data handling within a thread, see Thread-Safe Resource Manager as a Thread Isolation Facility.
In addition, the Zend engine has to face a very special usage pattern: within a relatively short period of time, many zval structure-sized memory blocks and other small memory blocks are applied for and then released. PHP's memory management also attaches great importance to memory_limit (memory limit).
Note: Unlike the similar parts of the C standard library, if there is an error in allocating the requested memory, the Zend engine's memory management function will not return a NULL value, but will jump out and abort the current request. .
As mentioned above, preventing memory leaks and releasing all memory as quickly as possible is an important part of memory management. For security reasons, the Zend engine releases all memory allocated by the API mentioned above at the end of the request. This will produce a warning if PHP is built with the --enable-debug configuration option.
PHP Leak Alarm
ZEND_FUNCTION(leak) { long leakbytes = 3; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &leakbytes) == FAILURE) { return; } emalloc(leakbytes); }
The above is the detailed content of Several areas of php memory. For more information, please follow other related articles on the PHP Chinese website!