PHP Kernel (a) memory management, PHP kernel memory management _php Tutorial

Source: Internet
Author: User
Tags php website

PHP Kernel (a) memory management, PHP kernel memory management


First, the Memory Management Foundation

When developing with the C language, developers have to manually perform memory management. PHP is often used as a module for Web servers, and memory management is closely associated with preventing memory leaks. In addition, PHP may be used in a threading environment, so global variables can lead to competitive situations. In addition, the Zend engine faces a very special usage pattern: In a relatively short period of time, many zval-sized memory blocks and other small memory blocks are applied and freed, and PHP memory management attaches great importance to memory_limit (memory limit)

To meet the above requirements, the Zend Engine provides a special memory manager for processing request-related data. Request-related data refers to data that only needs to be served on a single request, at the latest, at the end of the request. Extension developers are primarily exposed to the conventions listed in the following table, although some of the handy features provided are implemented using macros, but this article treats them like functions.

As mentioned above, preventing memory leaks and freeing all memory as quickly as possible is an important part of memory management. For security reasons, at the end of the request, the Zend engine releases all the memory allocated by the API mentioned above. If PHP is built using the--enable-debug configuration option, this will produce a warning

When using PHP variables, you need to confirm that the memory of the variable is allocated using Emalloc, and note the reference count.

Memory leak detection can only discover leaks caused by emalloc allocated memory blocks. For deep analysis, it is recommended to use a memory detector, such as Valgrind or Libumem. To simplify this analysis, you can disable PHP's memory manager by setting the environment variable use_zend_alloc=0 at PHP startup.

(The above is the PHP website Chinese content)

Second, data persistence

Data persistence means that any data is expected to last longer than the current request, and the memory manager without the Zend engine is very concerned about request binding allocations, but this is usually not practical or appropriate. Persistent memory is sometimes needed to meet the requirements of an external class library, and it is also a useful "black technology".

Persistent memory is often used in persistent database connections, although not good practice, but remains the most commonly used feature.

Note: The following all functions take an additional persistence parameter should be false, the engine will be using a regular allocator (EMALLOC), the memory should not be considered persistent (will not translate!). )。 As persistent memory, the system calls the allocator, just like the main memory API, in most cases they still do not return null pointers

Warning: It is important to note that the memory that is allocated for persistence is not optimized or is tracked by the Zend engine, it is not limited by memory_limit, and all variables created through the hacker must not be used to persist memory.

(the translation is really rotten!) )

http://www.bkjia.com/PHPjc/1094590.html www.bkjia.com true http://www.bkjia.com/PHPjc/1094590.html techarticle PHP Kernel (a) memory management, PHP kernel memory management One, the memory management base in C language development, developers have to manually do memory management. PHP is often used as a Web server module, 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.