opened image

PHP System Garbage Collector on Linux

System garbage collector

 

The system garbage collector (Garbage Collector) is an essential component of web applications, including PHP, and is used to automatically remove unused data and resources in memory, such as session files and logs. In PHP, garbage collector settings are controlled by directives in the php.ini file.

One such directive is `session.gc_probability`, which determines whether the garbage collector is likely to run during each request. This probability is calculated as the ratio of `gc_probability` to `gc_divisor`, i.e. a value of `1/10` means that the garbage collector will run in one out of ten requests, or with a 10% chance per request. This directive only applies if the session handler is set to `Files` and the session files are saved in the directory specified in the `Session.save_path` directive.

To change the value of `session.gc_probability` in the configuration file `/etc/php.d/apache/php.ini`, you need to edit this file to set the required value. By default, this value is 0, which means that the garbage collector will not be automatically started.

After making changes to the PHP configuration file, you need to restart the Apache web server for the new settings to take effect. This can be done by running `/etc/init.d/apache2 restart` on the command line.

Also, when working with web applications that have a heavy load on the server, it can be useful to set a time delay (`session.gc_maxlifetime`) in seconds, after which session data will be considered "garbage" and potentially removed by the garbage collector. For example, setting `session.gc_maxlifetime = 1440` specifies that session data that has not been updated for 1440 seconds (or 24 minutes) will be deleted.

 

Cleaning up logs

To free up disk space and remove old archived journals, there are `journalctl` commands on a Linux system that allow you to configure and clean up the journals.

The command is used to clean up archived logs so that the volume occupied by the logs on disk does not exceed the specified value (for example, 100 MB):

journalctl --vacuum-size=100M

The command cleans up the archived logs, leaving only the data on the disk for the last specified time period (for example, 1 day):

journalctl --vacuum-time=1d

Similarly, the command removes the logs, leaving only the last week's data:

journalctl --vacuum-time=1weeks

These commands are useful for managing log volume and freeing up disk space, especially on systems where logs can take up a lot of disk space.