Introduction to php reference counter for garbage collection_PHP tutorial

WBOY
Release: 2016-07-21 15:16:08
Original
927 people have browsed it

PHP has a very simple garbage collector that will actually garbage collect objects that are no longer in the memory scope. The internal way garbage collection works is to use a reference counter, so when the counter reaches 0 (meaning that no reference to the object is available), the object is garbage collected and removed from memory.

Every computer language has its own automatic garbage collection mechanism, so that programmers don’t have to worry too much about program memory allocation. PHP is no exception. However, in object-oriented programming (OOP) programming, some objects need to be displayed. Destruction style; prevent program execution memory from overflowing.

1. PHP Garbage Collector (GC)

In PHP, when no variable points to the object, the object becomes garbage. PHP will destroy it in memory; this is PHP's GC garbage disposal mechanism to prevent memory overflow.

When a PHP thread ends, all memory space currently occupied will be destroyed, and all objects in the current program will be destroyed at the same time. The GC process generally starts running with each SESSION. The purpose of gc is to automatically destroy and delete the session files after they expire.

2. __destruct /unset

__destruct() The destructor is executed when the garbage object is recycled.
unset destroys the variable pointing to the object, not the object.

3. Session and GC

Due to the working mechanism of PHP, it does not have a daemon thread to regularly scan the Session information and determine whether it is invalid. When a valid When a request occurs, PHP will decide whether to enable a GC based on the values ​​of the global variables session.gc_probability and session.gc_divisor. By default, session.gc_probability=1, session.gc_divisor =100 means that there is 1% Possibility to start GC (that is, only one GC out of 100 requests will be started with one of the 100 requests).

The job of GC is to scan all Session information and subtract the session from the current time The last modified time is compared with the session.gc_maxlifetime parameter. If the survival time exceeds gc_maxlifetime (default 24 minutes), the session will be deleted.
However, if your web server has multiple sites, GC may have unexpected results when processing sessions at multiple sites. The reason is: when GC is working, it does not distinguish between sessions of different sites.

So how to solve it at this time?

1. Modify session.save_path, or use session_save_path() to save the session of each site to a dedicated directory,
2. Provide GC startup rate, naturally, GC startup rate If it is increased, the system performance will be reduced accordingly, which is not recommended.
3. Determine the survival time of the current session in the code and delete it using session_destroy()

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325967.htmlTechArticlePHP has a very simple garbage collector that actually collects objects that are no longer in the memory scope (scope) Objects are garbage collected. The internal way garbage collection is done is to use a reference counter...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template