Home Backend Development PHP Tutorial A brief discussion of PHP source code 33: Basics of the new garbage collection mechanism (Garbage Collection) added in PHP5.3

A brief discussion of PHP source code 33: Basics of the new garbage collection mechanism (Garbage Collection) added in PHP5.3

Jun 29, 2018 am 10:00 AM

This article mainly introduces the basics of PHP source code thirty-three: the newly added garbage collection mechanism (Garbage Collection) of PHP5.3. It has a certain reference value. Now I share it with you. Friends in need can refer to it. Let’s talk about

33 of PHP source code: The basics of the new garbage collection mechanism (Garbage Collection) in PHP5.3
The garbage collection mechanism is newly added in PHP5.3, which is said to be very advanced. That said seduced me to see its advanced implementation.
For the official documentation, please click Garbage Collection
Chinese version address: http://docs.php.net/manual/zh/features.gc.php
[Embedding method of garbage collection mechanism]## The #zend_gc.h file is referenced at line 749 of zend.h: #include "zend_gc.h"
Thus replacing the ALLOC_ZVAL and other macros in the zend_alloc.h file referenced at line 237
zend/zend_gc. h file starts at line 202

1

2

3

4

5

/* The following macroses override macroses from zend_alloc.h */#undef  ALLOC_ZVAL#define ALLOC_ZVAL(z) \

do {\

(z) = (zval*)emalloc(sizeof(zval_gc_info));\

GC_ZVAL_INIT(z);\

} while (0)

Copy after login

The definition of ALLOC_ZVAL macro in zend_alloc.h is to allocate the memory space of a zval structure. The new ALLOC_ZVAL macro allocates a zval_gc_info structure macro. The structure of zval_gc_info is as follows:

zend/zend_gc.h file starts at line 91:

1

2

3

4

5

6

typedef struct _zval_gc_info {

zval z;

union {

gc_root_buffer       *buffered;

struct _zval_gc_info *next;

} u;} zval_gc_info;

Copy after login

The first member of zval_gc_info is the zval structure, which ensures that it is aligned with the beginning of the memory allocated with the zval variable , so that it can be used as a zval when the zval_gc_info type pointer is cast. The gc_root_buffer, etc. will be introduced later in the structure and implementation. It defines the cache structure of the PHP garbage collection mechanism. GC_ZVAL_INIT is used to initialize zval_gc_info that replaces zval. It will set the buffered field of member u in zval_gc_info to NULL. This field will only have a value when it is put into the garbage collection buffer, otherwise it will always be NULL.

Since all variables in PHP exist in the form of zval variables, zval_gc_info is used here to replace zval, thereby successfully integrating the garbage collection mechanism in the original system.

This feels a bit like object-oriented polymorphism.

[Storage method of garbage collection mechanism]

Node structure:

1

2

3

4

5

6

7

8

typedef struct _gc_root_buffer {

struct _gc_root_buffer   *prev;/* double-linked list               */

struct _gc_root_buffer   *next;

zend_object_handle        handle;/* must be 0 for zval               */

union {

zval                 *pz;

zend_object_handlers *handlers;

} u;} gc_root_buffer;

Copy after login

Obviously (see comments, although there are very few comments in PHP, some are purely tangled comments) , which is a doubly linked list.

The pz variable in the union is obviously the polymorphic zval_gc_info structure defined before, so its current node pointer in the linked list can be passed ((zval_gc_info*)(pz))->u .buffered, but looking at the source code, this calling method is used in many places, why not create a new macro? Is it because I am afraid of having too many macros? No, PHP is famous for having many macros, and there are many macros that have more nested macros than this one. don't know. In addition, handle and other structures are specifically targeted at object variables.

The buffer is in a global variable. Like the global variables of other modules, gc also has its own global variable access macro GC_G(v). Similarly, the global variable access macro is different under ZTS or not. realization.

The global variables defined in zend_gc.h are as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

typedef struct _zend_gc_globals {

zend_bool         gc_enabled;/* 是否开启垃圾收集机制 */

zend_bool         gc_active;/* 是否正在进行 */

gc_root_buffer   *buf;/* 预分配的缓冲区数组,默认为10000(preallocated arrays of buffers)   */

gc_root_buffer    roots;/* 列表的根结点(list of possible roots of cycles) */

gc_root_buffer   *unused;/* 没有使用过的缓冲区列表(list of unused buffers)           */

gc_root_buffer   *first_unused;/* 指向第一个没有使用过的缓冲区结点(pointer to first unused buffer)   */

gc_root_buffer   *last_unused;/* 指向最后一个没有使用过的缓冲区结点,此处为标记结束用(pointer to last unused buffer)    */

zval_gc_info     *zval_to_free;/* 将要释放的zval变量的临时列表(temporaryt list of zvals to free) */

zval_gc_info     *free_list;/* 临时变量,需要释放的列表开头 */

zval_gc_info     *next_to_free;/* 临时变量,下一个将要释放的变量位置*/

zend_uint gc_runs;/* gc运行的次数统计 */

zend_uint collected;    /* gc中垃圾的个数 */ // 省略...

Copy after login

[Color marking in the garbage collection mechanism]

1

2

3

4

5

6

7

#define GC_COLOR  0x03 #define GC_BLACK  0x00#define GC_WHITE  0x01#define GC_GREY   0x02#define GC_PURPLE 0x03 #define GC_ADDRESS(v) \

((gc_root_buffer*)(((zend_uintptr_t)(v)) & ~GC_COLOR))#define GC_SET_ADDRESS(v, a) \

(v) = ((gc_root_buffer*)((((zend_uintptr_t)(v)) & GC_COLOR) | ((zend_uintptr_t)(a))))#define GC_GET_COLOR(v) \

(((zend_uintptr_t)(v)) & GC_COLOR)#define GC_SET_COLOR(v, c) \

(v) = ((gc_root_buffer*)((((zend_uintptr_t)(v)) & ~GC_COLOR) | (c)))#define GC_SET_BLACK(v) \

(v) = ((gc_root_buffer*)(((zend_uintptr_t)(v)) & ~GC_COLOR))#define GC_SET_PURPLE(v) \

(v) = ((gc_root_buffer*)(((zend_uintptr_t)(v)) | GC_PURPLE))

Copy after login

In PHP's memory management, we have also seen similar The last bit serves as some type of marking method.

Here, the last two digits of the memory allocation are used as the color mark of the entire structure. Among them,

white means garbage
purple means it has been put into the buffer
gray means that a refcount minus one operation has been performed
black is the default color, normal

[zval defined Change】

PHP3.0 version is in the zend/zend.h file, which is defined as follows:

1

2

3

4

5

6

struct _zval_struct {

/* Variable information */

zvalue_value value;/* value */

zend_uint refcount__gc;

zend_uchar type;/* active type */

zend_uchar is_ref__gc;};

Copy after login

In versions before php3.0, such as php5.2.9 version, it is in the zend/zend.h file , its definition is as follows:

1

2

3

4

5

6

struct _zval_struct {

/* Variable information */

zvalue_value value;/* value */

zend_uint refcount;

zend_uchar type;/* active type */

zend_uchar is_ref;};

Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

A brief discussion on PHP source code 32: emalloc/efree layer and heap layer in PHP memory pool

A brief discussion on PHP source code 31: The basics of the heap layer in the PHP memory pool

A brief discussion on PHP source code 30: PHP memory pool Storage layer in

The above is the detailed content of A brief discussion of PHP source code 33: Basics of the new garbage collection mechanism (Garbage Collection) added in PHP5.3. For more information, please follow other related articles on the PHP Chinese website!

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An article to talk about the garbage collection mechanism in php An article to talk about the garbage collection mechanism in php Aug 26, 2022 am 10:48 AM

This article will give you an in-depth understanding of the garbage collection mechanism in PHP. I hope it will be helpful to you!

What is php source code What is php source code Oct 11, 2019 am 09:35 AM

PHP source code refers to PHP source code. Source code is the basis of programs and websites, and PHP, the "hypertext preprocessor", is a general open source scripting language.

Decrypting the memory management and garbage collection mechanism of Go language Decrypting the memory management and garbage collection mechanism of Go language Nov 30, 2023 am 09:17 AM

Go language is an efficient, safe, and concurrent programming language. The design of memory management and garbage collection mechanism is also its unique feature. This article will decrypt the memory management and garbage collection mechanism of Go language in depth. 1. Memory management In the Go language, memory management includes two aspects: memory allocation and memory release. 1.1 Memory allocation In the Go language, we allocate memory through the built-in functions new and make. Among them, new returns a pointer to the newly allocated zero value, while make returns a specified type and its length.

How to solve common problems of memory release in Java functions? How to solve common problems of memory release in Java functions? May 02, 2024 am 09:57 AM

Memory management in Java involves garbage collection, but problems can still arise. Common problems include memory leaks and memory fragmentation. Memory leaks are caused by objects holding references that are no longer needed and can be solved by avoiding reference cycles, using weak references, and limiting variable scope. Memory fragmentation is caused by frequent allocation and deallocation and can be solved by using memory pools, large object pools, and compact garbage collection. For example, using weak references can handle memory leaks and ensure that the garbage collector reclaims objects when they are no longer needed.

PHP source code running problem: index error solution PHP source code running problem: index error solution Mar 09, 2024 pm 09:24 PM

PHP source code running problem: Index error resolution requires specific code examples. PHP is a widely used server-side scripting language that is often used to develop dynamic websites and web applications. However, sometimes you will encounter various problems when running PHP source code, among which "index error" is a common situation. This article will introduce some common causes and solutions of index errors, and provide specific code examples to help readers better deal with such problems. Problem Description: When running a PHP program

In-depth understanding of the underlying development principles of PHP: memory management and garbage collection mechanism In-depth understanding of the underlying development principles of PHP: memory management and garbage collection mechanism Sep 10, 2023 pm 02:30 PM

In-depth understanding of the underlying development principles of PHP: memory management and garbage collection mechanism Introduction: PHP, as a high-level programming language, is widely used in Web development. Many developers are familiar with PHP's syntax and features, but may have relatively little understanding of PHP's underlying development principles. This article will deeply explore the memory management and garbage collection mechanisms in the underlying development principles of PHP to help readers better understand the operating mechanism of PHP. 1. PHP’s memory management Memory allocation and release Memory management in PHP is handled by the Zend engine

An in-depth analysis of the garbage collection mechanism in Python An in-depth analysis of the garbage collection mechanism in Python Mar 29, 2018 pm 01:20 PM

Thanks to Python's automatic garbage collection mechanism, there is no need to manually release objects when creating them in Python. This is very developer friendly and frees developers from having to worry about low-level memory management. But if you don’t understand its garbage collection mechanism, the Python code you write will often be very inefficient.

Explore the memory management features and garbage collection mechanism of Go language Explore the memory management features and garbage collection mechanism of Go language Jan 23, 2024 am 10:07 AM

Exploring the garbage collection mechanism and memory management features of Go language Introduction: With the development of the Internet, developers have increasingly higher requirements for programming languages. As a statically typed, compiled language, Go language has attracted much attention since its inception due to its efficient garbage collection mechanism and memory management features. This article aims to deeply explore the garbage collection mechanism of the Go language and its memory management features, and help readers better understand and utilize these features through specific code examples. 1. Garbage collection mechanism 1.1 mark-scan algorithm of Go language

See all articles