session garbage collection mechanism in php

  • 2021-10-24 19:04:48
  • OfStack

In PHP, when no variables point to this object, this object becomes garbage. PHP will destroy it in memory; This is the GC garbage disposal mechanism of PHP to prevent memory overflow.

The job of GC is to scan all Session information, subtract the last modification time of session from the current time, compare it with session.gc_maxlifetime parameter, and delete session if the lifetime exceeds gc_maxlifetime (default 24 minutes).

When a valid request occurs, PHP will decide whether to enable an 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, which means that there is a 1% chance of starting GC (that is, only one gc out of 100 requests will be started with one of the 100 requests).

However, if your Web server has multiple sites, GC may have unexpected results when processing session, because GC does not distinguish session from different sites when working.

So how to solve it at this time?

Modify session. save_path, or use session_save_path () to save session for each site to a dedicated directory,

Provide GC startup rate, naturally, GC startup rate increases, the system performance will be reduced accordingly, not recommended.

Judge the lifetime of the current session in the code and delete it with session_destroy ().

session.gc_probability = 1
session.gc_divisor = 100
session.gc_maxlifetime = 1440

These three configurations combine to build the garbage collection mechanism of session on the server side

session.gc_probability and session.gc_divisor constitute the probability of performing session cleaning. Theoretically, the server has a fixed probability to call gc function to clean session at regular intervals, and the cleaning probability is: gc_probability/gc_divisor. For example, 1/100 means that every new session initializes, there is a 1% probability of starting garbage collection program. The cleaning standard is the time defined by session.gc_maxlifetime.
session. gc_divisor and session. gc_probability together define the probability of starting the gc (garbage collection garbage collection) process at each session initialization. This probability is calculated using gc_probability/gc_divisor. For example, 1/100 means that there is a 1% probability of starting the gc process in each request. session. gc_divisor defaults to 100.
For example: session.gc_maxlifetime=30, session.gc_divisor=1000, session.gc_probability=1, which means that when every 1,000 users call session_start (), they will execute a garbage collection mechanism 100%.

Note: For 1 large portal sites, it is recommended to increase session.gc_divisor by 1 point to reduce overhead. Next, I will use an example to demonstrate how to configure the process to call gc (garbage collection)!

Modify the following information by configuring the php. ini file:

session. gc_maxlifetime = 60//If the session file is not accessed after 60s, the session file will be treated as a "junk file" and cleaned up while waiting for the call of the gc (garbage collection) process, session.gc_probability = 1000, because the probability of the gc process being called is calculated through gc_probability/gc_divisor, where I changed session.gc_probability to 1000, and session.gc_divisor is also 1000 by default. The gc process is called each time the session_start () function is executed.

Open 3 sessions, Three corresponding session files are created, When each file is not called within 30 seconds, It will be regarded as "junk files", When the gc process calls, The "junk file" will be used by unlink, Because I have changed the php. ini configuration file before, Change the probability of gc being called to 100%, So next, if I reuse any one browser to refresh the next page, only one of the three session files should be left. save_handler = files, which defaults to file. Define the way session is saved on the server side. file means saving sesion to a temporary file. If we want to customize other ways to save it (such as using a database), we need to set this item to user.


Related articles: