Some readers may have encountered the following errors:
Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y bytes) This error message is clear, PHP has reached the maximum memory Allowed, generally, this may be a problem in programming. For example, if a large file is read to the memory at a time, an array is large, or a variable that is not used in a large loop is not released in time, these may cause excessive memory usage and termination.
The default maximum memory size of PHP is 32 MB. If you need to use a memory larger than 32 MB, you can modify the following configuration in the php. ini configuration file:
Memory_limit = 32 m if you cannot modify the php configuration file and the ini_set () function is not disabled in your PHP environment, you can also dynamically modify the maximum memory usage:
<? Php ini_set ("memory_limit", "128 M"); since we can dynamically adjust the maximum memory usage, is there a way to obtain the current memory usage? The answer is yes.
1. memory_get_usage ()This function is used to obtain the memory size currently used by the PHP script.
2. memory_get_peak_usage (), The function returns the memory peak occupied by the current script to the current position, so that the current script memory requirements may be obtained.
For the functions provided by PHP user space, we seem to be unable to control memory usage and can only passively obtain memory usage
So when we know that php reference counting, function tables, symbol tables, constant tables, and so on, this information will occupy the memory
We can intentionally avoid unnecessary waste of memory. For example, we usually use autoload in projects to avoid one-time inclusion of classes that may not be used, and this information will occupy the memory.
If we unset a variable that is no longer used in time, the space occupied by the variable may be released.