search
HomePHP FrameworkSwooleWill swoole memory become larger and larger?

Will swoole memory become larger and larger?

Server memory management mechanism

The underlying principle of memory management after Server is started is the same as that of the ordinary php-cli program. For details, please refer to the Zend VM memory management article.

local variables (Recommended learning: SWOOLE Video Tutorial )

## After the event recovery function returns, all local objects and variables will be recovered. No need to unset. If the variable is a resource type, the corresponding resource will also be released by the bottom layer of PHP.

function test()
{
    $a = new Object;
    $b = fopen('/data/t.log', 'r+');
    $c = new swoole_client(SWOOLE_SYNC);
    $d = new swoole_client(SWOOLE_SYNC);
    global $e;
    $e['client'] = $d;
}

$a, $b, $c are all local variables. When this function returns, these three variables will be released immediately, the corresponding memory will be released immediately, and the opened IO resource file handle will be closed immediately .

$d is also a local variable, but it is saved to the global variable $e before return, so it will not be released. When unset($e['client']) is executed, and no other PHP variables are still referencing the $d variable, $d will be released.

Global variables

In PHP, there are 3 types of global variables.

Variables declared using the global keyword

Class static variables and function static variables declared using the static keyword

Super global variables of PHP, including $_GET, $_POST , $GLOBALS, etc.

Global variables and objects, class static variables, variables saved on the Server object will not be released. Programmers need to handle the destruction of these variables and objects themselves.

class Test
{
    static $array = array();
    static $string = '';
}
function onReceive($serv, $fd, $reactorId, $data)
{
    Test::$array[] = $fd;
    Test::$string .= $data;
}

You need to pay special attention to the array type value of non-local variables in the event callback function. Certain operations such as TestClass::$array[] = "string" may cause memory leaks, and in severe cases, memory explosion may occur. , care should be taken to clean up large arrays when necessary.

In the event callback function, when concatenating strings of non-local variables, you must be careful about memory leaks, such as TestClass::$string .= $data, there may be memory leaks, and in severe cases, memory explosion may occur. .

The above is the detailed content of Will swoole memory become larger and larger?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.