Dive into the PHP memory related features

  • 2020-06-12 08:37:07
  • OfStack

Some readers may have encountered the following mistakes:
Fatal error: Allowed memory of bytes exhausted (tried to allocate Y bytes) The error message is clear, PHP has reached the maximum allowed memory, and this is usually due to some programming problem. For example: once read a large file into memory, or appear a large array, or in the large loop is not in time is no longer used variables, these may cause excessive memory consumption and will be terminated.

The default maximum memory usage of PHP is 32M. If you really need to use more than 32M, you can modify the configuration of the ES22en. ini configuration file as follows:

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 change the maximum memory footprint:

< ? memory_limit php ini_set (" ", "128 M"); Since we can dynamically adjust the maximum footprint, is there a way to get the current footprint? The answer is yes.

memory_get_usage(), which gets the memory size of the current PHP script.

ak_usage(), this function returns the peak memory occupied by the current script to the current location, so it is possible to obtain the current script's memory requirements.
In terms of the functionality provided by PHP userspace alone, it seems that we have no control over memory usage and can only passively capture memory usage

So when we know that the php reference count, the function table, the symbol table, the constant table, all of that stuff takes up memory

We can intentionally avoid unnecessary memory waste. For example, autoload is commonly used in projects to avoid the one-time use of including classes that will be used, and this information will consume memory

If we get rid of the variable unset that we no longer use in time, we might be able to free up the space that it takes up

Related articles: