|
Basic memory managementWhen programming in the C programming language the developer has to manually care about memory management. As PHP is usually used as a web server module memory management is from special relevance in order to prevent memory leaks. Additionally you should be aware that PHP might be used in threaded environments which means that global variables might lead to race conditions. For information about dealing with thread-global data please refer to the documentation on the Thread-Safe Resource Manager, which serves as thread-isolation facility. Additionally to these requirements the Zend Engine is faced with a quite special usage pattern where, within a relatively short time, many memory blocks of the size of a zval structure or other small memory blocks are requested and freed again. Memory management in PHP has also to respect the memory_limit. For fulfilling the above requirements the Zend Engine provides a special memory manager for handling request-bound data. Request-bound data is data that is needed only for serving a single request and which will be freed at the request's end at latest. Extension authors will mostly only have contact with the routines listed in the table below. Although they are implemented as macros for providing some convenience features this documentation will treat them like functions.
As said above it is an essential part of the memory management to avoid
having memory leaks and therefore free all memory you've allocated as soon
as possible. As a safety net the Zend Engine will free all memory, which had
been allocated using above mentioned APIs at the end of a request. If PHP
was built using the Пример #1 PHP's leak warnings ZEND_FUNCTION(leak) { long leakbytes = 3; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &leakbytes) == FAILURE) { return; } emalloc(leakbytes); } Результатом выполнения данного примера будет что-то подобное: [Thu Oct 22 02:14:57 2009] Script: '-' /home/johannes/src/PHP_5_3/Zend/zend_builtin_functions.c(1377) : Freeing 0x088888D4 (3 bytes), script=- === Total 1 memory leaks detected ===
|
|||||||||||||||||