php数组的限制于memory_limit的关系
前几天在服务器上处理一个大文件时,遇到了一个问题
<br /> PHP Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 10255856 bytes) in /usr/local/asg/www/scripts/sync.php on line 641<br />
在网上看了下,说是跟php.ini里设置的memory_limit有关,因为那个脚本中有个函数,是把文件的每行经过处理后放入一个数组的,那个文件有46419行,我想可能是超过了限制吧,于是我在windos系统做了下测试
<br /> $arr = array();<br /> for($i=0;$i=1590000;$i++) {<br /> $arr[] = $i;<br /> }<br /> <br /> echo count($arr);<br />
php.ini中的memory_limit设置为
<br /> ; Maximum amount of memory a script may consume (128MB)<br /> ; http://php.net/memory-limit<br /> memory_limit = 128M<br />
运行后,显示
<br /> Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes) in D:\server\www\02\array.php on line 5<br />
之后,我便把数组调小了些
<br /> $arr = array();<br /> <br /> for($i=0;$i<1550000;$i++) {<br /> $arr[] = $i;<br /> }<br /> <br /> <br /> echo count($arr);<br />
这次可以显示了,长度为150000
接着我又改了下php.ini中memory_limit大小
<br /> ; Maximum amount of memory a script may consume (128MB)<br /> ; http://php.net/memory-limit<br /> memory_limit = 512M<br />
运行了下,之前报错的那个数据现在可以显示了,数组的长度为1590000
我想问下,php.ini里的memory_limit和数组长度是什么关系,当然了从问题上看,可以知道前者的大小决定后者的长度,如何根据前者的大小测试数组长度的上限呢?
------解决方案--------------------
跟长度没有关系。。
是你脚本执行时使用的内存有关
------解决方案--------------------
我想看了这个,你应该知道点什么了
$t = 0;<br /> echo memory_get_usage() - $t, ' ',$t = memory_get_usage(), PHP_EOL;<br /> $a = array();<br /> echo memory_get_usage() - $t, ' ', $t = memory_get_usage(), PHP_EOL;<br /> $a[] = 1;<br /> echo memory_get_usage() - $t, ' ', $t = memory_get_usage(), PHP_EOL;<br /> $a[] = 2;<br /> echo memory_get_usage() - $t, ' ', $t = memory_get_usage(), PHP_EOL;<br /> $a[] = 3;<br /> echo memory_get_usage() - $t, ' ', $t = memory_get_usage(), PHP_EOL;<br />
------解决方案--------------------
数组大概占多少内存,可以估算出来。比如你存放的是4字节的int数,数组有1000个元素就是4*1000字节。如果是字符串,按字符估算个平均值既可。
当然memory_limit设置的值不光是你一个数组所占用的内存数,php本身进程启动,以及加载那些扩展、还有你程序中其他地方也需要占用内存。
------解决方案--------------------
是说企图再分配24个字节时,超过了128M的内存限制, 所以失败了.

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
