PHP内核(1)内存管理
PHP内核(一)内存管理
本文链接:http://www.orlion.ml/tag/php-internal/
一、内存管理基础
用c语言开发时,开发者要手动进行内存管理。PHP经常作为web服务器的模块,内存管理与预防内存泄露紧密关联。另外PHP可能用于线程环境中,所以全局变量可能导致竞争情况。此外Zend引擎面对一个十分特殊的使用模式:在一段比较短的时间内,许多zval结构大小的内存块和其他的小内存块被申请又再被释放,PHP的内存管理也很重视memory_limit(内存限制)
为了满足以上的需求,Zend引擎提供了为了处理请求相关数据提供了一种特殊的内存管理器。请求相关数据是指只需要服务于单个请求,最迟在请求结束时释放的数据。扩展开发者主要接触下表中列出的惯例,虽然一些所提供的便捷功能使用宏实现的,但是本文中会像函数一样对待。
如上所述,防止有内存泄露并尽可能快的释放所有内存是内存管理的重要组成部分。因为安全原因,在请求结束时,Zend引擎会释放所有由上面提到的API所分配的内存。如果PHP使用--enable-debug配置选项进行构建,这将产生一个警告
当使用PHP变量时,需要确认变量的内存要使用emalloc来分配,并注意引用计数。
内存泄漏检测仅可以发现由 emalloc 分配内存块导致的泄漏。为进行深层分析,建议使用内存检测器,如 valgrind 或 libumem 等。要简化此分析,可在 PHP 启动时通过设置环境变量 USE_ZEND_ALLOC=0 来禁用 PHP 的内存管理器。
(以上是PHP官网中文内容)
二、数据持久化
数据持久化意味着任何数据预计比当前请求生存时间长,没有Zend引擎的内存管理器非常关注于请求绑定分配,但是这通常不是实用或者是合适的。持久化内存有时需要为了满足外部类库的要求,它也是有用的"黑科技"。
持久化内存通常用在持久化数据库连接上,虽然实践起来并不好,但依然是最常使用的特性。
注意:下面所有函数采取额外的持久化参数应该是false,引擎将用常规的分配器(emalloc),内存不应该是 considered persistent(不会翻译!)。作为持久化的内存,系统调用分配器,正像主要的内存API一样在大多数情况下它们仍然不返回空指针
警告:需要注意被分配用来持久化的内存不是最优化的或者是被Zend引擎跟踪的,它不被memory_limit所限制,另外,所有通过the hacker创建的变量一定不能被用来持久化内存。
(翻译的真烂!)
- 1楼tianxintian22
- 原来是翻译的,怪不得没读懂︿( ̄︶ ̄)︿

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

AI Hentai Generator
Generate AI Hentai for free.

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,
