设置PHP的memory_limit的大小

设置PHP的memory_limit的大小

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 1099 bytes) in

解释是可用内存已耗尽,这关系到PHP的memory_limit的设置问题。

这里有两种方法解决

1、修改php.ini

Php代码

memory_limit = 128

这种方法需要重启服务器,很显然,此方法对虚拟机有限制。

2、通过ini_set函数修改配置选项值

Php代码

ini_set ('memory_limit', '128M')

很显然, 这种方法更合适。

=================================

以下是配置选项的简要解释。memory_limit integer

本指令设定了一个脚本所能够申请到的最大内存字节数。这有助于防止写得不好的脚本消耗光服务器上的可用内存。要使用此指令必须在编译的时候激活。因此 configure 一行中应该包括:–enable-memory-limit。如果不需要任何内存上的限制,必须将其设为 -1。

自 4.3.2 起,当激活了 memory_limit,PHP 函数 memory_get_usage() 便可以使用了。

当使用 integer 类型时,其值以字节为度量单位。还可以用简化符号,说明见此 FAQ。

在phpchina的论坛上,有人这样说:

memory_limit是设置PHP使用内存时空间,一般设置为内存的四分之一,应该就查不多了。如512M内存设置128M,1G内存设置256M。设置太大会影响系统速度,因为系统和数据库及其他程序同样需要内存空间,一般系统和数据库内存空间是自己分配的。

==============================

Fatal error: Out of memory (allocated 786432) (tried to allocate 98304 bytes) in H:\freehost\zhengbao2\web\includes\lib_common.php on line 744

疑问:786432 是指786432bytes?即:768MB 98304 bytes=96MB 那0.75M是怎么算出来的。

正确答案:786432bytes=768KB=0.75MB 98304bytes=96KB=0.09MB ?

最小是位 <其次字节(byte)<KB<MB<GB<TB

一个字节 =8位

1KB=1024B(字节)

1MB=1024KB

1GB=1024MB

1TB=1024GB

千,1KB=2的10次方=1024B,

兆,1MB=2的20次方=1024*1024B=1024KB,

吉,1GB=2的30次方=1024*1024*1024B=1024MB,

太,1TB=2的40次方=1024*1024*1024*1024B=1024GB,

拍,1PB=2的50次方=1024*1024*1024*1024*1024B=1024TB,

艾,1EB=2的60次方=1024*1024*1024*1024*1024*1024B=1024PB,

泽,1ZB=2的70次方=1024*1024*1024*1024*1024*1024*1024B=1024EB,

尧,1YB=2的80次方=1024*1024*1024*1024*1024*1024*1024*1024B=1024ZB.

今天一个客户的网站出现这样的错误。程序是PHP的。经百度了一下。说明内存不足。中文件翻译的意思是:内存不足(分配786432)(尝试分配98304字节)

意思是尝试分配98MB的内存。但一般php设置为只允许给php程序分配 0.75M内存

解决办法:打开php.ini 修改memory_limit = 的这行

依照服务器配置进行调整

配置的内存使用大小修改后重启iis/apache

问题解决^^

PHP5.2.3-win32这个版本有个非常严重的bug,那就是最大内存设置成1024M,但是PHP实际返回的只有2M,经常返回下面的错误:

PHP Fatal error: Out of memory (allocated 1048576) (tried to allocate 393216 bytes)

PHP Fatal error: Out of memory (allocated 1048576) (tried to allocate 393216 bytes)

PHP Fatal error: Out of memory (allocated 1048576) (tried to allocate 393216 bytes)

PHP Fatal error: Out of memory (allocated 1048576) (tried to allocate 393216 bytes)

PHP Fatal error: Out of memory (allocated 786432) (tried to allocate 393216 bytes)

PHP Fatal error: Out of memory (allocated 1310720) (tried to allocate 393216 bytes)

这是官方的bug报告,该bug只出现于windows版本,linux版本没有这个bug。看来只能等下一次升级的时候修复了。 PHP5.2.3-win32 我的是PHP5.2.8-win32 怎么还是会偶尔出现?