简体   繁体   English

PHP 5.3.3中的ini_set(“ memory_limit”)根本不起作用

[英]ini_set(“memory_limit”) in PHP 5.3.3 is not working at all

I had this working before : 我以前有这个工作:

echo ini_get("memory_limit")."\n";
ini_set("memory_limit","256M");
echo ini_get("memory_limit")."\n";

That would input this : 那将输入:

32M
256M

on a php script executed by command line. 在命令行执行的php脚本上。 I updated from 5.2 to 5.3, and from now, this directive is not working at all : this gives me : 我从5.2更新到5.3,从现在开始,此指令完全不起作用:这给了我:

32M
32M

and then make my script fail with a fatal Error... 然后让我的脚本因致命错误而失败...

I checked the php documentation, and googled it, and I didn't find anywhere that "memory_limit" had been deprecated. 我检查了php文档,并对其进行了谷歌搜索,没有发现“ memory_limit”已被弃用的任何地方。

Does anyone have a solution? 有没有人有办法解决吗?

If you have the suhosin extension enabled, it can prevent scripts from setting the memory limit beyond what it started with or some defined cap. 如果启用了suhosin扩展名,它可以防止脚本将内存限制设置为超出其开头或某些定义的上限。

http://www.hardened-php.net/suhosin/configuration.html#suhosin.memory_limit http://www.hardened-php.net/suhosin/configuration.html#suhosin.memory_limit

Most likely your sushosin updated, which changed the default of suhosin.memory_limit from disabled to 0 (which won't allow any updates to memory_limit). 最有可能是您的sushosin已更新,从而将suhosin.memory_limit的默认值从Disabled更改为0(不允许对memory_limit进行任何更新)。

On Debian, change /etc/php5/conf.d/suhosin.ini 在Debian上,更改/etc/php5/conf.d/suhosin.ini

;suhosin.memory_limit = 0 ; suhosin.memory_limit = 0

to

suhosin.memory_limit = 2G suhosin.memory_limit = 2G

Or whichever value you are comfortable with. 或您满意的任何价值。 You can find the changelog of Sushosin at http://www.hardened-php.net/hphp/changelog.html , which says: 您可以在http://www.hardened-php.net/hphp/changelog.html上找到Sushosin的变更日志,其中显示:

Changed the way the memory_limit protection is implemented 更改了memory_limit保护的实现方式

Here's a list of things that are worth checking: 以下是值得检查的事项列表:

Is Suhosin installed? 是否安装了Suhosin?

ini_set ini_set

  • The format is important ini_set('memory_limit', '512'); // DIDN'T WORK ini_set('memory_limit', '512MB'); // DIDN'T WORK ini_set('memory_limit', '512M'); // OK - 512MB ini_set('memory_limit', 512000000); // OK - 512MB 格式很重要ini_set('memory_limit', '512'); // DIDN'T WORK ini_set('memory_limit', '512MB'); // DIDN'T WORK ini_set('memory_limit', '512M'); // OK - 512MB ini_set('memory_limit', 512000000); // OK - 512MB ini_set('memory_limit', '512'); // DIDN'T WORK ini_set('memory_limit', '512MB'); // DIDN'T WORK ini_set('memory_limit', '512M'); // OK - 512MB ini_set('memory_limit', 512000000); // OK - 512MB

When an integer is used, the value is measured in bytes. 使用整数时,该值以字节为单位。 Shorthand notation, as described in this FAQ , may also be used. 也可以使用本常见问题解答中所述的速记符号。

http://php.net/manual/en/ini.core.php#ini.memory-limit http://php.net/manual/zh/ini.core.php#ini.memory-limit

  • Has php_admin_value been used in .htaccess or virtualhost files? php_admin_value是否已在.htaccess或virtualhost文件中使用?

Sets the value of the specified directive. 设置指定指令的值。 This can not be used in .htaccess files. 这不能在.htaccess文件中使用。 Any directive type set with php_admin_value can not be overridden by .htaccess or ini_set(). .htaccess或ini_set()不能覆盖任何用php_admin_value设置的指令类型。 To clear a previously set value use none as the value. 要清除先前设置的值,请​​不使用任何值。

http://php.net/manual/en/configuration.changes.php http://php.net/manual/zh/configuration.changes.php

Works for me, has nothing to do with PHP 5.3. 对我有效,与PHP 5.3无关。 Just like many such options it cannot be overriden via ini_set() when safe_mode is enabled. 与许多此类选项一样,启用safe_mode时不能通过ini_set()覆盖它。 Check your updated php.ini (and better yet: change the memory_limit there too). 检查您更新的php.ini (更好的是:也在那里更改memory_limit)。

Ubuntu 10.04 comes with the Suhosin patch only, which does not give you configuration options. Ubuntu 10.04仅随附Suhosin补丁,该补丁不提供配置选项。 But you can install php5-suhosin to solve this: 但是您可以安装php5-suhosin来解决此问题:

apt-get update
apt-get install php5-suhosin

Now you can edit /etc/php5/conf.d/suhosin.ini and set: 现在,您可以编辑/etc/php5/conf.d/suhosin.ini并设置:

suhosin.memory_limit = 1G

Then using ini_set will work in a script: 然后使用ini_set将在脚本中工作:

ini_set('memory_limit', '256M');

Let's do a test with 2 examples: 让我们用两个示例进行测试:

    <?php    
    $memory = (int)ini_get("memory_limit"); // Display your current value in php.ini (for example: 64M)
    echo "original memory: ".$memory."<br>";
    ini_set('memory_limit','128M'); // Try to override the memory limit for this script
    echo "new memory:".$memory;
    }
    // Will display:
    // original memory: 64
    // new memory: 64
    ?>

The above example doesn't work for overriding the memory_limit value. 上面的示例不适用于覆盖memory_limit值。 But This will work: 但这将起作用:

    <?php
    $memory = (int)ini_get("memory_limit"); // get the current value
    ini_set('memory_limit','128'); // override the value
    echo "original memory: ".$memory."<br>"; // echo the original value
    $new_memory = (int)ini_get("memory_limit"); // get the new value
    echo "new memory: ".$new_memory;  // echo the new value
    // Will display:
    // original memory: 64
    // new memory: 128
    ?>

You have to place the ini_set('memory_limit','128M'); 您必须放置ini_set('memory_limit','128M'); at the top of the file or at least before any echo . 文件的开头至少在回声之前

As for me, suhosin wasn't the solution because it doesn't even appear in my phpinfo(), but this worked: 对于我来说, suhosin并不是解决方案,因为它甚至都没有出现在我的phpinfo()中,但是这样做有效:

    <?php
    ini_set('memory_limit','2048M'); // set at the top of the file
    (...)
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM