49

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. I updated from 5.2 to 5.3, and from now, this directive is not working at all : this gives me :

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.

Does anyone have a solution?

hakre
  • 193,403
  • 52
  • 435
  • 836
Ant
  • 1,812
  • 5
  • 22
  • 39

6 Answers6

28

Here's a list of things that are worth checking:

Is Suhosin installed?

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

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

  • Has php_admin_value been used in .htaccess or virtualhost files?

Sets the value of the specified directive. This can not be used in .htaccess files. Any directive type set with php_admin_value can not be overridden by .htaccess or ini_set(). To clear a previously set value use none as the value.

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

Community
  • 1
  • 1
Carlton
  • 5,533
  • 4
  • 54
  • 73
27

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).

On Debian, change /etc/php5/conf.d/suhosin.ini

;suhosin.memory_limit = 0

to

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:

Changed the way the memory_limit protection is implemented

4wk_
  • 2,458
  • 3
  • 34
  • 46
  • very helpful! The suhosin docs for this setting indicate that it's an integer and don't clearly communicate that you can use the (MG) suffixes; thus I was thrown off until I read this. – matt Jun 08 '12 at 03:25
25

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.

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

Oscar M.
  • 1,076
  • 7
  • 9
4

Ubuntu 10.04 comes with the Suhosin patch only, which does not give you configuration options. But you can install php5-suhosin to solve this:

apt-get update
apt-get install php5-suhosin

Now you can edit /etc/php5/conf.d/suhosin.ini and set:

suhosin.memory_limit = 1G

Then using ini_set will work in a script:

ini_set('memory_limit', '256M');
Appmerce
  • 493
  • 4
  • 10
4

Works for me, has nothing to do with PHP 5.3. Just like many such options it cannot be overriden via ini_set() when safe_mode is enabled. Check your updated php.ini (and better yet: change the memory_limit there too).

mario
  • 144,265
  • 20
  • 237
  • 291
0

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. 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'); 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:

    <?php
    ini_set('memory_limit','2048M'); // set at the top of the file
    (...)
    ?>
Lechampi
  • 21
  • 2
  • 1
    You have a flaw in your code, thus your conclusion is wrong! – DUzun Apr 18 '16 at 09:04
  • @DUzun can you explain where is the "flaw" in my code ? (i don't know what is a "flaw", i'm not native English speaker). An explaination of your point, for my code which works (i've tested it, and i use it), would be better than just a -1. Thx – Lechampi Apr 19 '16 at 10:12
  • @DUzun i have modified the example that i tested again with a blank php file. My conclusion is a bit different but my previous solution is still working. – Lechampi Apr 19 '16 at 11:16