Home > Article > Backend Development > Detailed explanation of PHP memory-related features_PHP Tutorial
Some readers may have encountered errors similar to the following:
Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y bytes )The message of this error is very clear. PHP has reached the maximum memory allowed to be used. Generally speaking, this is likely to be some problem with our program writing. For example: reading a very large file into memory at one time, or a very large array, or not releasing unused variables in a timely manner in a large loop, all of which may cause excessive memory usage and be terminated.
PHP’s default maximum memory usage is 32M. If you really need to use more than 32M of memory, you can modify the following configuration in the php.ini configuration file:
memory_limit = 32M If you cannot modify the php configuration file and your PHP environment does not disable the ini_set() function, you can also dynamically modify the maximum memory size:
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.
As far as the functions provided by PHP user space alone, it seems that we cannot control the memory usage and can only passively obtain the memory usage
So when we know that PHP reference count, function table, symbol table, constant table, etc., these information will occupy memory
We can intentionally avoid unnecessary waste of memory. For example, we usually use autoload in projects to avoid including classes that are not necessarily used at one time, and this information will occupy memory
If we unset variables that are no longer used in time, the space occupied by them may be released