The PHPunset () function destroys the variable. We are learning that the PHPunset () function is used to destroy Variables. However, many times, this function only destroys the variables, and the values of the variables stored in the memory are still not destroyed, that is, we are not learning
The PHP unset () function is used to destroy a variable. However, in many cases, this function only destroys the variable. The value of the variable stored in the memory is still not destroyed, that is, it fails to achieve the desired effect of releasing memory. Here, we recommend that you use the $ variable = null method to release its memory. The reason is as follows.
The following are some important points about the PHP unset () function: (The following are all tested in windows, php 2.5.9)
1. this function only releases memory when the space occupied by variable values exceeds 256 bytes.
2. the address will be released only when all the variables pointing to this value (for example, a referenced variable pointing to this value) are destroyed (the judgment of 1 is also required)
The following is an example of code demonstration:
- <? Php
- $ Test = str_repeat ("1", 256 );
- $ S = memory_get_usage ();
- // Modify the function to view the memory currently used
- Unset ($ test );
- $ E = memory_get_usage ();
- Echo 'release memory: '. ($ s-$ e );
- // The output is 272, but if the above test variable is changed
$ Test = str_repeat ("1", 255). The output value is 0.
- ?>
As for why it is 272 instead of 256, it is not very clear and does not know how to handle it internally.
- <? Php
- $ Test = str_repeat ("1", 256 );
- $ P = & $ test;
- Unset ($ test );
- Echo $ p;
- // The output value is 256. If the above is changed to unset ($ p)
, Even worse, echo $ test is directly displayed as 256 1
- ?>
That is to say, the value assigned to $ a in the memory still exists. It can be seen that unset () does not release the memory.
However, if $ test = null is added to the above code or an unset ($ p) is added, the memory can be released. The PHP unset () function test code is as follows:
To assign a value to a variable, use the following method:
- <? Php
- $ Test = str_repeat ("1", 256 );
- $ P = & $ test;
- $ S = memory_get_usage ();
- $ Test = null;
- Unset ($ test );
- $ E = memory_get_usage ();
- Echo 'release memory: '. ($ s-$ e );
- // The output is 272.
- Var_dump ($ p); // The output is NULL.
- ?>
To destroy all the variables that point to the value of this address:
- <? Php
- $ Test = str_repeat ("1", 256 );
- $ P = & $ test;
- $ S = memory_get_usage ();
- // Note: The following two unset () sequence does not match
The relationship does not affect the result.
- Unset ($ p );
- Unset ($ test );
- $ E = memory_get_usage ();
- Echo 'release memory: '. ($ s-$ e); // The output is 272.
- ?>
The PHP unset () function has been demonstrated.
The reset PHP unset () function is used to destroy the variable. However, in many cases, this function only destroys the variable, and the value of the variable stored in the memory is still not destroyed, that is, no...