Some people say that PHP's unset does not really release memory, some say, PHP unset only in the release of large variables (a large number of strings, large arrays) when the real free memory, but also said that in the PHP level to discuss memory is meaningless.
It was also said that:
The unset () function frees up memory space only if the variable value occupies more than 256 bytes of memory space.
Memory is freed only when all variables that point to the variable, such as reference variables, are destroyed.
So, let's take a look at the following example:
[PHP] view plain copy print? $s =str_repeat (' 1 ', 255); Produces a string composed of 255 1 $m =memory_get_usage (); Gets the currently occupied memory unset ($s); $MM =memory_get_usage (); Unset () and then view the current memory usage echo $m-$mm;
Windows php5.5 output 264, when the $s=str_repeat (' 1 ', 256) is in the above example, the output 273 indicates that the php5.5 version of unset frees up memory and does not have a 256-byte limit.
Let's take another look at the following example:
[PHP] view plain copy print? $s =str_repeat (' 1 ', 256); This is exactly the same as the second example $p =& $s; $m =memory_get_usage (); Unset ($s); Destruction of $s $mm =memory_get_usage (); echo $p. ' <br/> '; echo $m-$mm;
Window php5.5 output 256 1, second row-48. Can be seen in time to destroy the variable $s, do not destroy $p did not release memory.
When the unset ($s) is added to the unset ($p), output 320 is shown to show that the memory is freed, meaning that only when all references to the variable are destroyed will the memory be truly freed.
In order to break the casserole ask the end, we are talking about the variable memory allocation from PHP:
First of all, we need to know that PHP's memory allocation is implicit, not as the C language shows the call memory allocation API will allocate memory.
For example, we define variables: $i = ' how are you! ';
There are two procedures for implicit allocation: 1. Allocate memory for a variable and save it in a symbol table. 2. Allocate memory for variable values.
Let's take a look at how PHP variables are represented.
PHP is a weakly typed, dynamic scripting language. The so-called weak type, that is, PHP does not strictly verify the variable type (strictly speaking, PHP is a medium-strong type language, this part will be described in future articles), when declaring a variable, do not need to show the type of data it holds:
[PHP] view plain copy print? $var = 1; int $var = "Laruence"; string $var = 1.0002; float $var = Array (); Array $var = new Exception (' Error '); Object Dynamic language, that is, the language structure of PHP can be changed during the runtime, such as we require a function definition file at runtime, which results in the dynamic change of the function table of the language.
The so-called scripting language, that is, PHP is not run independently, to run PHP we need the PHP parser. PHP is executed through the Zend engine (ze, Zend engine), Ze is written in C, and everyone knows that C is a strongly typed language, that is, all variables in C can only hold one type of data when it is declared to be destroyed. So how does PHP implement a weak type on ze basis?
In PHP, all variables are saved with a struct-zval, and in zend/zend.h we can see the definition of Zval:
[PHP] view plain copy print?