PHP kernel--memory leak and new garbage collection mechanism ____php

Source: Internet
Author: User
Tags garbage collection
The garbage collection mechanism is a dynamic storage allocation scheme. It automatically frees allocated blocks of memory that the program no longer needs. The process of automatically reclaiming memory is called garbage collection. The garbage collection mechanism allows programmers to devote more effort to business logic without being overly concerned with program memory allocations. PHP also implements dynamic memory management in the language layer, which has been described in detail in the previous chapters, and the dynamic management of memory frees developers from cumbersome memory management. With this support, PHP also provides a language layer of garbage collection mechanism, so that programmers do not have to be overly concerned about the program memory allocation. Prior to the PHP5.3 version, PHP had a simple garbage collection based on reference counting, and when the reference count of a variable changed to 0 o'clock, PHP would destroy the variable in memory, except that the rubbish was not called garbage. and PHP releases the content of this process/thread at the end of a lifecycle, which determines that PHP does not need to think too much about memory leaks in the early stages. But as PHP grows, so does the growth of PHP developers and the expansion of the scope of business it carries. In PHP5.3, a more perfect garbage collection mechanism is introduced. The new garbage collection mechanism resolves a reference memory leak problem that cannot handle loops. Below, we will work with the example test data to explore the new garbage collection mechanism.
(What Tips:refcount and Zval are.) Each variable is saved in a zval structure at the bottom of the PHP, with the same worth of variables sharing a value and refcount to the number of variables, such as $a= $b = 1, then they point to a zval with a value of 1,refount=2, indicating that two variables point to it, Learn more about the storage mechanism (detach/change) of the PHP kernel below is a test of the actual data, and the conclusion is in the lower right corner:

how to avoid memory leaks: 1. Write high-quality code, reduce the possibility of memory leaks 2. The system automatically reclaims memory based on the PHP lifecycle (valid for very rinit, Rshotdown) 3. Manual recycle, that is, reboot the server nginx or Apache (Minit, Mshotdown ) 4. php.ini configuration file: php.ini memory_limit = 128M (not recommended)   Fatal error:allowed memory size of, 134217728 bytes exhausted (t ried to allocate 38218371 bytes)  128MB = 134217728byte

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.