Home >Backend Development >PHP Tutorial >Introduction to memory management in php

Introduction to memory management in php

不言
不言forward
2019-03-27 09:43:562861browse

This article brings you an introduction to memory management in php. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Overview of PHP memory management - Zend engine

Since the computer's memory is managed by the operating system, ordinary applications cannot directly access the memory.

Applications can only apply for memory from the operating system. Common applications do the same and apply for memory from the operating system through library functions such as malloc when needed.

In some application scenarios with high performance requirements, memory needs to be frequently used and released, such as web servers, programming languages, etc. Since applying for memory space from the operating system will trigger system calls, system calls and The performance difference of ordinary application layer function calls is very different, because system calls will switch the CPU from user mode to kernel, because only the operating system can perform operations involving physical memory, and the cost of this switch is very large. If Frequent switching between kernel mode and user mode can cause performance problems.

In view of the overhead of system calls, some applications that have performance requirements usually perform memory management in user mode. For example, when applying for a slightly larger memory for the first time, it is reserved, and the memory released after use is not Immediately return it to the operating system, and the memory can be reused, thus avoiding the performance consumption caused by multiple memory applications and releases.

PHP does not need to explicitly manage memory, these tasks are managed by the Zend engine. There is a memory management system inside PHP, which will automatically release memory garbage that is no longer used.

2. View and set memory-related parameters and functions in php

Configure memory size:

(1) The configuration memory_limit = 32M## can be changed in php.ini #(2) If the ini_set() function is not disabled in the environment, you can set it through this function: ini_set("memory_limit", "128M");

Check the memory status:

(1) memory_get_usage(), the function of this function is to obtain the memory size currently used by the PHP script.

(2) memory_get_peak_usage(), the function of this function returns the peak memory occupied by the current script to the current position, so that it is possible to obtain the memory requirements of the current script.

This article has ended here. For more other exciting content, you can pay attention to the

php video tutorial column of the PHP Chinese website!

The above is the detailed content of Introduction to memory management in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete