


A brief discussion of PHP source code 33: Basics of the new garbage collection mechanism (Garbage Collection) added in PHP5.3
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
/* 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)
zend/zend_gc.h file starts at line 91:
typedef struct _zval_gc_info { zval z; union { gc_root_buffer *buffered; struct _zval_gc_info *next; } u;} zval_gc_info;
This feels a bit like object-oriented polymorphism.
Node structure:
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;
The global variables defined in zend_gc.h are as follows:
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中垃圾的个数 */ // 省略...
#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))
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
PHP3.0 version is in the zend/zend.h file, which is defined as follows:
struct _zval_struct { /* Variable information */ zvalue_value value;/* value */ zend_uint refcount__gc; zend_uchar type;/* active type */ zend_uchar is_ref__gc;};
struct _zval_struct { /* Variable information */ zvalue_value value;/* value */ zend_uint refcount; zend_uchar type;/* active type */ zend_uchar is_ref;};
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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.

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 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.

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 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

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.

Primitive types are stored on the stack, and reference types are stored on the heap. JavaScript automatically allocates memory when variables (objects, strings, etc.) are created, and "automatically" releases them when they are not used. The process of releasing is called garbage collection.
