PHP內(nèi)核(1)內(nèi)存管理
Jun 13, 2016 pm 12:28 PM
PHP內(nèi)核(一)內(nèi)存管理
本文鏈接:http://www.orlion.ml/tag/php-internal/
一、內(nèi)存管理基礎(chǔ)
用c語言開發(fā)時(shí),開發(fā)者要手動(dòng)進(jìn)行內(nèi)存管理。PHP經(jīng)常作為web服務(wù)器的模塊,內(nèi)存管理與預(yù)防內(nèi)存泄露緊密關(guān)聯(lián)。另外PHP可能用于線程環(huán)境中,所以全局變量可能導(dǎo)致競(jìng)爭情況。此外Zend引擎面對(duì)一個(gè)十分特殊的使用模式:在一段比較短的時(shí)間內(nèi),許多zval結(jié)構(gòu)大小的內(nèi)存塊和其他的小內(nèi)存塊被申請(qǐng)又再被釋放,PHP的內(nèi)存管理也很重視memory_limit(內(nèi)存限制)
為了滿足以上的需求,Zend引擎提供了為了處理請(qǐng)求相關(guān)數(shù)據(jù)提供了一種特殊的內(nèi)存管理器。請(qǐng)求相關(guān)數(shù)據(jù)是指只需要服務(wù)于單個(gè)請(qǐng)求,最遲在請(qǐng)求結(jié)束時(shí)釋放的數(shù)據(jù)。擴(kuò)展開發(fā)者主要接觸下表中列出的慣例,雖然一些所提供的便捷功能使用宏實(shí)現(xiàn)的,但是本文中會(huì)像函數(shù)一樣對(duì)待。
如上所述,防止有內(nèi)存泄露并盡可能快的釋放所有內(nèi)存是內(nèi)存管理的重要組成部分。因?yàn)榘踩?,在?qǐng)求結(jié)束時(shí),Zend引擎會(huì)釋放所有由上面提到的API所分配的內(nèi)存。如果PHP使用--enable-debug配置選項(xiàng)進(jìn)行構(gòu)建,這將產(chǎn)生一個(gè)警告
當(dāng)使用PHP變量時(shí),需要確認(rèn)變量的內(nèi)存要使用emalloc來分配,并注意引用計(jì)數(shù)。
內(nèi)存泄漏檢測(cè)僅可以發(fā)現(xiàn)由 emalloc 分配內(nèi)存塊導(dǎo)致的泄漏。為進(jìn)行深層分析,建議使用內(nèi)存檢測(cè)器,如 valgrind 或 libumem 等。要簡化此分析,可在 PHP 啟動(dòng)時(shí)通過設(shè)置環(huán)境變量 USE_ZEND_ALLOC=0 來禁用 PHP 的內(nèi)存管理器。
(以上是PHP官網(wǎng)中文內(nèi)容)
二、數(shù)據(jù)持久化
數(shù)據(jù)持久化意味著任何數(shù)據(jù)預(yù)計(jì)比當(dāng)前請(qǐng)求生存時(shí)間長,沒有Zend引擎的內(nèi)存管理器非常關(guān)注于請(qǐng)求綁定分配,但是這通常不是實(shí)用或者是合適的。持久化內(nèi)存有時(shí)需要為了滿足外部類庫的要求,它也是有用的"黑科技"。
持久化內(nèi)存通常用在持久化數(shù)據(jù)庫連接上,雖然實(shí)踐起來并不好,但依然是最常使用的特性。
注意:下面所有函數(shù)采取額外的持久化參數(shù)應(yīng)該是false,引擎將用常規(guī)的分配器(emalloc),內(nèi)存不應(yīng)該是?considered persistent(不會(huì)翻譯!)。作為持久化的內(nèi)存,系統(tǒng)調(diào)用分配器,正像主要的內(nèi)存API一樣在大多數(shù)情況下它們?nèi)匀徊环祷乜罩羔?/p>
警告:需要注意被分配用來持久化的內(nèi)存不是最優(yōu)化的或者是被Zend引擎跟蹤的,它不被memory_limit所限制,另外,所有通過the hacker創(chuàng)建的變量一定不能被用來持久化內(nèi)存。
(翻譯的真爛!)
- 1樓tianxintian22
- 原來是翻譯的,怪不得沒讀懂︿( ̄︶ ̄)︿

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

ThePhpfunctionSerialize () andunserialize () AreusedtoconvertcomplexdaTastructdestoresintostoraSandaBackagain.1.Serialize () c OnvertsdatalikecarraysorobjectsraystringcontainingTypeandstructureinformation.2.unserialize () Reconstruct theoriginalatataprom

You can embed PHP code into HTML files, but make sure that the file has an extension of .php so that the server can parse it correctly. Use standard tags to wrap PHP code, insert dynamic content anywhere in HTML. In addition, you can switch PHP and HTML multiple times in the same file to realize dynamic functions such as conditional rendering. Be sure to pay attention to the server configuration and syntax correctness to avoid problems caused by short labels, quotation mark errors or omitted end labels.

The key to writing clean and easy-to-maintain PHP code lies in clear naming, following standards, reasonable structure, making good use of comments and testability. 1. Use clear variables, functions and class names, such as $userData and calculateTotalPrice(); 2. Follow the PSR-12 standard unified code style; 3. Split the code structure according to responsibilities, and organize it using MVC or Laravel-style catalogs; 4. Avoid noodles-style code and split the logic into small functions with a single responsibility; 5. Add comments at key points and write interface documents to clarify parameters, return values ??and exceptions; 6. Improve testability, adopt dependency injection, reduce global state and static methods. These practices improve code quality, collaboration efficiency and post-maintenance ease.

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas
