PHP kernel (1) memory management, php kernel memory management _ PHP Tutorial

Source: Internet
Author: User
PHP kernel (1) memory management and php kernel memory management. PHP kernel (I) memory management, php kernel memory management I. When the memory management is developed in C language, developers need to manually manage the memory. PHP is often used as a web server module. memory: PHP kernel (1) memory management and php kernel memory management

I. memory management basics

When using C language development, developers must manually manage the memory. PHP is often used as a web server module. memory management is closely related to preventing memory leaks. In addition, PHP may be used in the thread environment, so global variables may cause competition. In addition, the Zend Engine has a very special usage mode: in a short period of time, many memory blocks with the zval structure and other small memory blocks are applied for and released again, memory management in PHP also attaches great importance to memory_limit (memory limit)

To meet the preceding requirements, the Zend Engine provides a special memory manager for processing request-related data. Request-related data refers to the data that only needs to serve a single request and is released at the end of the request at the latest. Extended developers are mainly familiar with the conventions listed in the following table. although some convenient functions are implemented using macros, this article will treat them like functions.

As mentioned above, preventing memory leakage and releasing all memory as quickly as possible is an important part of memory management. For security reasons, when the request ends, the Zend Engine releases all memory allocated by the API mentioned above. If PHP uses the -- enable-debug configuration option for building, this generates a warning.

When using PHP variables, make sure that the memory of the variables should be allocated using emalloc, and pay attention to the reference count.

Memory leakage detection can only detect the leakage caused by memory blocks allocated by emalloc. For deep analysis, we recommend that you use a memory detector, such as valgrind or libumem. To simplify this analysis, you can disable the PHP memory manager by setting the environment variable USE_ZEND_ALLOC = 0 at PHP startup.

(The above is the Chinese content on the PHP official website)

II. data persistence

Data persistence means that any data is expected to survive longer than the current request. the memory manager without the Zend Engine is very concerned with request binding and allocation, but this is usually not practical or suitable. Persistent memory sometimes needs to meet the requirements of external class libraries. it is also useful for "black Technology ".

Persistent memory is usually used for persistent database connections. although it is not good in practice, it is still the most commonly used feature.

Note: The Extra persistence parameter of all the following functions should be false. the engine will use the regular Alloc and the memory should not be considered persistent (no translation !). As persistent memory, the system calls the distributor, just like the main memory API, in most cases they still do not return null pointers.

Warning: The memory allocated for persistence is not optimized or tracked by the Zend Engine. it is not restricted by memory_limit. In addition, all variables created using the hacker cannot be used for persistent memory.

(Translation is really bad !)

Memory (1) memory management, php kernel memory management 1. when the memory management basics are developed in C language, developers need to manually manage the memory. PHP is often used as a web server module, with memory...

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.