Will variables that are no longer used in PHP be recycled immediately?

Source: Internet
Author: User
{Code ...}
$ Huge_var = "Long long string variable ..... "; // use or do not use this sentence $ huge_var = null; // there are a lot of code below, but it has nothing to do with $ huge_var // a lot of code // The code is about to end. At this moment, is the memory occupied by $ huge_var recycled? Does the interpreter have such optimization operations during preprocessing? // Does the usage of $ huge_var = null affect the memory usage of the entire PHP script at this moment?

Reply content:
$ Huge_var = "Long long string variable ..... "; // use or do not use this sentence $ huge_var = null; // there are a lot of code below, but it has nothing to do with $ huge_var // a lot of code // The code is about to end. At this moment, is the memory occupied by $ huge_var recycled? Does the interpreter have such optimization operations during preprocessing? // Does the usage of $ huge_var = null affect the memory usage of the entire PHP script at this moment?

If your variable is a global variable$GLOBALS['huge_var']If you do not useunset($huge_var)Release, it will be recycled only after the script is run. but even if you don't release it, it doesn't matter when running in PHP-FPM or MOD_PHP, because the PHP-FPM will release all the resources after the request ends, so there will be no memory leakage. however, if you write CLI command line scripts, especially Daemon (while(true){}), You should use unset to release unused variables to avoid Memory leakage.


  

It can be seen that unset can release the system memory occupied by PHP processes, especially when releasing large arrays or long strings.

You do not need to manually release the local variables in the function. The local variables are automatically released after the function is executed.$GLOBALS['huge_var']Or global $ huge_var can access your global variables.

 

Memory_get_usage returns the memory allocated to the PHP script, in bytes, excluding the memory of the PHP process.
Memory_get_peak_usage returns the peak memory allocated to the PHP script.

PHP5 garbage collection mechanism:
Http://php.net/manual/zh/features.gc.php
Each PHP variable is stored in a variable container named "zval.

$ A = 'new string'; // when a variable is assigned a constant value, a zval variable container xdebug_debug_zval ('A') is generated; // output: (refcount = 1, is_ref = 0) = string (10) "new string"; $ B = $ a; xdebug_debug_zval ('A'); // output: (refcount = 2, is_ref = 0) = string (10) "new string" unset ($ B); xdebug_debug_zval ('A'); // output: (refcount = 1, is_ref = 0) = string (10) "new string"

A variable container with the string type and the new string value is generated.
In the additional two bytes, is_ref is set to FALSE by default because no custom reference is generated.
Refcount is set to 1 because only one variable uses this variable container.
If we execute unset ($ a), the variable container containing the type and value will be deleted from the memory.

If a reference count increases, it will continue to be used, and of course it will not be in the garbage.
If the reference count is reduced to zero, the variable container will be cleared (free ).
That is to say, garbage cycle is generated only when the reference count is reduced to a non-zero value ).
Second, in a garbage cycle, check whether the reference count is reduced by 1, and check which variable containers reference zero times to find which part is garbage.
In php. ini, the cyclic reference collector is activated by default:
Zend. enable_gc = On
PHP can use gc_enabled to determine whether a loop exists. You can use gc_enable/gc_disable to explicitly activate or disable the circular reference collector.
Gc_collect_cycles can be called even if the root buffer is not full.
The reason that the garbage collection mechanism is allowed to be opened and disabled and self-Initialization is allowed is that some part of your application may be highly time-sensitive.
In this case, you may not want to use the garbage collection mechanism.
Of course, disabling the garbage collection mechanism for some part of your application is at risk of Memory leakage, because some root may not be saved into a limited root buffer.
Therefore, it may be wise to call the gc_collect_cycles () function before you call the gc_disable () function to release the memory.
This will clear all possible roots that have been stored in the root buffer. When the garbage collection mechanism is disabled, an empty buffer can be left to have more storage space.

if(gc_enabled()) { gc_collect_cycles(); gc_disable();}

PHP Memory leakage refers to the failure of GC collection because you keep the introduction of variables.

Impact of PHP garbage collection on performance:
The first is to save memory space, and the other is to increase the execution time when the garbage collection mechanism cleans up memory.

Why does disabling garbage collection (GC) significantly improve the operational efficiency of composer?
Since PHP garbage collection is based on reference counting, In order to recycle objects that are referenced cyclically, it will try to detect and recycle the island objects that are referenced cyclically when the reference counting is reduced but less than 0, however, when the number of valid objects and mutual references are large (for example, the generation table packages, versions, and dependencies in composer), the overhead of such search will become very huge, this results in a large amount of CPU computing. Composer creates a large number of objects in the memory during running. These objects trigger the GC mechanism, and these objects need to be used, so GC cannot be cleared. Therefore, disabling GC using gc_disable can save CPU time and improve efficiency.

I seldom care about this because the script is executed and nothing is done unless it is a persistent connection.
Even if it is released, it is just the difference between now and a while, and it is not like a java container running endlessly.

After the test, the answer is that it will not be recycled immediately.

Add $ var = null to reclaim the memory, write it as an anonymous variable, or use the function scope.

// The code is about to end. At this moment, is the memory occupied by $ huge_var recycled?

Obviously not. You can continue to use the $ huge_var variable here, right?

$huge_var = null;

The above sentence will release the memory. If there is really "a lot of code", it is best to use this sentence.

In addition, if $ huge_var is really large, you should think about it. Is $ huge_var really necessary?
You can consider using PHP "stream" related functions.

In php, when a process starts to execute, the engine will first apply for a piece of memory, which is not enough for running.
If a variable is unset or assigned null in the middle, the memory occupied by the variable will be recycled, but it will not be returned to the system memory and will still be retained by the engine. Only when the process ends, the memory occupied by php will be released.
Therefore, when the operation is prone to overhead memory, you can split the process into multiple processes for processing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.