Home > Backend Development > PHP Tutorial > Introduction to php reference counter for garbage collection mechanism

Introduction to php reference counter for garbage collection mechanism

高洛峰
Release: 2023-03-02 16:34:01
Original
894 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 do not have to worry too much about program memory allocation, and PHP is no exception. However, in object-oriented programming (OOP) programming, some objects need to be explicitly destroyed; Prevent program execution memory from overflowing.

1. PHP Garbage Collector (GC)

In PHP, when no variables point to this 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 used when the garbage object is Executed during recycling.
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 Session information and determine whether it is invalid. When a valid request occurs, PHP will use the global variables session.gc_probability and The value of session.gc_divisor determines whether to enable a GC. By default, session.gc_probability=1, session.gc_divisor =100 means there is a 1% possibility of starting a GC (that is, only one in 100 requests) gc will be started with one of the 100 requests).

The job of GC is to scan all Session information, subtract the last modification time of the session from the current time, and compare it 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, when there are multiple sites, GC may have unexpected results when processing sessions. The reason is: when GC is working, it will not distinguish between sessions of different sites.

So at this time How to solve it?

1. Modify session.save_path, or use session_save_path() to save the session of each site to a dedicated directory,
2. Provide the GC startup rate. Naturally, as the GC startup rate increases, the system performance will decrease accordingly. ,Not recommended.
3. Determine the survival time of the current session in the code and use session_destroy() to delete it. Please indicate the source for reprinting: Introduction to the garbage collection mechanism of php reference counter

Related labels:
php
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