We are learning
The PHP unset () function is used to destroy variables, but most of the time, the function only destroys the variable, and the value of the variable stored in the memory is still not destroyed, that is, it does not achieve the effect we want to release the memory. Here I recommend that you use the $ variable =null method to free its memory. The reason to see the following will know.
Here are a few points about the PHP unset () function: (The following are tested in the Windows environment, PHP 2.5.9)
1. The function frees memory only if the value of the variable takes up more than 256 bytes of space
2. The address is freed only if all variables that point to the value (for example, a reference variable points to that value) are destroyed (and a 1 judgment is performed)
Here's an example code argument:
- < ? php
- $ test = str_repeat ( "1", 256);
- $ s = memory_get_usage ();
- //Change function to view the current memory used
- unset ($test);
- $ e = Memory_get_usage ();
- Echo ' Frees Memory: '. ( $s-$e);
- //output is 272, but if the test variable above is changed to
$ test = str_repeat ("1", 255), output is 0
-
As for why 272 is not 256, it is not very clear, do not know how to deal with the internal.
- < ? PHP
- $ Test str_repeat("1", "n" );
- $ P = & $test;
- Unset ($test);
- Echo $p;
- The output is 256 x 1. If the above is changed to unset ($p)
And, more than that, the Echo $test shown directly as 256 x 1
- ?>
This means that the value assigned to $ A in memory still exists. Visible unset () does not achieve the effect of releasing memory.
However, if you add $test=null to the above code, or add a unset ($p), you can release the memory effect, the PHP unset () function test code is as follows:
The variable is assigned a null method:
- < ? PHP
- $ Test = str_repeat ("1", 256);
- $ P = & $test;
- $ s = Memory_get_usage ();
- $ Test = NULL ;
- Unset ($test);
- $ e = Memory_get_usage ();
- echo ' Free Memory: '. ($s-$e);
- Output is 272
- Var_dump ($p); Output is null
- ?>
method to destroy all variables that point to the value in this address:
- < ? php
- $ test = str_repeat (" 1 ", 256);
- $ p = & $test;
- $ s = Memory_get_usage ();
- //Note that the following 2 unset () Order reversal
has a relationship that does not affect the results
- unset ($p);
- unset ($test);
- $ e = Memory_get_usage ();
- Echo ' Frees Memory: '. ( $s-$e); Output is 272
- ?>
To this PHP unset () function argument is complete.
http://www.bkjia.com/PHPjc/445987.html www.bkjia.com true http://www.bkjia.com/PHPjc/445987.html techarticle We are learning PHP unset () function is used to destroy variables, but many times, this function only to destroy the variable, the memory stored in the value of the variable is still not destroyed, that is, no ...