-1

Laravel 4.2, Amazon ec2 Linux

PHP Fatal error:

Allowed memory size of 262144 bytes exhausted (tried to allocate 3072 bytes) in /var/www/html/vendor/composer/ClassLoader.php on line 78
elixenide
  • 42,388
  • 14
  • 70
  • 93
Marlon Buendia
  • 192
  • 3
  • 18
  • Change your `memory_limit` setting in php.ini. You're barely letting your scripts use any memory at all. – elixenide Oct 22 '15 at 04:38
  • 1
    Possible duplicate of [Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php](http://stackoverflow.com/questions/415801/allowed-memory-size-of-33554432-bytes-exhausted-tried-to-allocate-43148176-byte) – solarissmoke Oct 22 '15 at 04:41
  • @EdCottrell change into? – Marlon Buendia Oct 22 '15 at 04:44
  • @MarlonBuendia just about anything higher than 256k (your current setting). For most useful applications, you need at least a few MB. I would start with `8MB` and see how it goes. – elixenide Oct 22 '15 at 04:47
  • @EdCottrell I have ` ini_set('memory_limit', '1G');` in my php, will it work? – Marlon Buendia Oct 22 '15 at 04:51
  • @MarlonBuendia No. The shorthand only works in php.ini, not in `ini_set`. [See the docs](http://php.net/manual/en/faq.using.php#faq.using.shorthandbytes). Also, 1G is a bit excessive for most purposes. – elixenide Oct 22 '15 at 04:57
  • @EdCottrell So what shall I do change the memory_limit to 8MB etc/php.ini? – Marlon Buendia Oct 22 '15 at 06:49
  • @MarlonBuendia Yes, change it in your php.ini file, but make sure you found the right file. I have posted an answer that will help you do that. Please remember to accept it if you found this helpful! – elixenide Oct 22 '15 at 15:20
  • This is something you could have easily used a search engine to solve. – Josh J Oct 22 '15 at 15:26

1 Answers1

2

TL;DR You need to increase your memory_limit setting in php.ini. You're barely letting your scripts use any memory at all.

The Problem

The error message Allowed memory size of 262144... means you have a memory_limit setting of 256 kb. That is far too low. For most useful applications, you need at least a few MB. I would start with 8MB and see how it goes.

The Solution

Change the memory_limit setting. In theory, you can do this two ways: (1) edit php.ini or (2) use ini_set().

In practice, you cannot always use ini_set('memory_limit', value);. For one thing, some extensions, like suhosin, prevent scripts from setting the memory_limit in this way. For another, you have to be careful how you do it. For example, you proposed using ini_set('memory_limit', '1G');. But the shorthand (K, M, G) only works in php.ini, not in ini_set. You would have to enter an actual number of bytes (e.g., 1073741824). Also, 1G is pretty excessive for most purposes; very, very few non-malicious PHP scripts need anything like that. Even pretty heavy frameworks like WordPress typically run well within 64 MB or so, even with lots of plugins loaded up.

How to change the setting in php.ini

Figure out which php.ini file you are using (it's not uncommon to have several floating around, depending on how you installed things). You can do this in two ways:

  1. Put this in a .php file and run it: <?php phpinfo();
  2. On a command line, type php -i | grep php.ini. You should see a line that says something like Loaded Configuration File => /etc/php.ini (the output may vary by system, of course).

Edit the file you just found by changing the line that starts with memory_limit = to something more appropriate, like

memory_limit = 8M

Feel free to bump that number up as needed, but I would recommend starting small (not 1G) to prevent bringing your server to its knees accidentally.

One caveat: if this is a testing/development machine and you are running a debugger or profiler like xdebug, you may want to start much higher, and 1G is not insane. But don't start with such a high number on a production machine; work your way up to it.

Community
  • 1
  • 1
elixenide
  • 42,388
  • 14
  • 70
  • 93