关于 PHP 中巨型数据对象的内存开销有关问题的研究
关于 PHP 中巨型数据对象的内存开销问题的研究
首先请大家不要误会,不是我要发表对这个问题的什么研究成果,而是想请大家帮我一起来分析研究一下这个问题 :)
描述一下简化了的问题背景:在一个用 PHP 实现的网站中,所有的程序文件都在一开始包含了一个公共的文件 common.php。现在由于业务需要,在 common.php 中定义了一个“巨型”的数据对象(一个含有约 500k 个 int 值的 array 对象),比如 $huge_array = array(1,2,3,...,500000),并且在整个系统中对 $huge_array 只有“读”访问。假设系统需要持续稳定运行在 100 个并发请求的状态下。
问题 1:$huge_array 在 common.php 源文件中大概要占用 10M(这个姑且不算是问题),加载到内存中也许要占用 4M(只是估算一下,至于准确测量其尺寸,不是本文要讨论的要点)。问题在于,PHP 本身每处理一个 HTTP request,都是要启用一个独立的进程(或者是线程),那它是不是都要重新在内存中加载这个约 4M 的内存块呢?如果不能共享内存的话,那可能就要同时占用近 400M 的物理内存,无论在内存占用量还是内存访问效率方面,都是很不利的。
问题 2:当启用了某种缓存机制(比如 APC、XCache 等)的时候,我们知道这类缓存机制都具有对 opcode 进行缓存的能力,但似乎也只是减少了脚本编译环节的重复性工作,对于运行时的变量加载,是否也能起到共享内存的作用呢?希望它能起到一定的作用,毕竟那一大堆 int 值肯定也是作为 opcode 的一部分而存在的。
问题 3:如果上述借用 XCache 对 opcode 的缓存不能达到目的的话,那我直接操作 XCache 是否会有效呢?就是说,不把 $huge_array 写在 common.php 里,而是写到缓存引擎里。
本文意在通过分析研究,确定这种用法是否会导致内存使用瓶颈,如果有问题的话如何优化。当然,想尽办法减小这个巨型数据对象本身的尺寸是首先最值得考虑的,必须的,但那个属于数据结构和算法方面的话题,就不在本文中讨论了。欢迎大家发表一下自己对这个问题的分析观点,如果能给自己的观点设计一些可操作的测试验证方案就更好了,如果你没时间写代码,只要方案看上去合理,我愿意来写测试代码 ^_^
――――――――――――――――――――――――――――――――
基于CSDN论坛提供的插件扩展功能,自己做了个签名档工具,分享给大家,欢迎技术交流 :)

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

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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

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,

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

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
