Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 13965430 bytes)
PHPInfo shows that I have a memory_limit of 128M, so I'm confused as to why the error says I only have 64M. Is it possible for phpinfo to report incorrectly? Or for PHP to use two separate php.inis?
The error was being caused by an ini_set call in one of the primary php files that a co-worker of mine added without my knowledge.
PHP's config can be set in multiple places:
- master system
php.ini
(usually in /etc somewhere) - somewhere in Apache's configuration (httpd.conf or a per-site .conf file, via
php_value
) - CLI & CGI can have a different
php.ini
(use the commandphp -i | grep memory_limit
to check the CLI conf) - local .htaccess files (also
php_value
) - in-script (via
ini_set()
)
In PHPinfo's output, the "Master" value is the compiled-in default value, and the "Local" value is what's actually in effect. It can be either unchanged from the default, or overridden in any of the above locations.
Also note that PHP generally has different .ini files for command-line and webserver-based operation. Checking phpinfo()
from the command line will report different values than if you'd run it in a web-based script.
ini_set('memory_limit', '128M');
or
php.ini => memory_limit = 128M
or
php_value memory_limit 128M
I had same issue. I found the answer:
ini_set('memory_limit', '-1');
Note: It will take unlimited memory usage of server.
Update: Use this carefully as this might slow down your system if the PHP script starts using an excessive amount of memory, causing a lot of swap space usage. You can use this if you know program will not take much memory and also you don't know how much to set it right now. But you will eventually find it how much memory you require for that program.
You should always memory limit as some value as answered by @şarkı dinle
.
ini_set('memory_limit', '512M');
Giving unlimited memory is bad practice, rather we should give some max limit that we can bear and then optimise our code or add some RAMs.
The memory must be configured in several places.
Set memory_limit
to 512M:
sudo vi /etc/php5/cgi/php.ini
sudo vi /etc/php5/cli/php.ini
sudo vi /etc/php5/apache2/php.ini Or /etc/php5/fpm/php.ini
Restart service:
sudo service service php5-fpm restart
sudo service service nginx restart
or
sudo service apache2 restart
Finally it should solve the problem of the memory_limit
If you're sure you restarted Apache after configuring php.ini, then you might be looking at the wrong php.ini file
1 check current limit:
(in my os)php -i | grep limit => memory_limit => 256M => 256M
2 locate php.ini
php --ini =>
Configuration File (php.ini) Path: /etc
Loaded Configuration File: /etc/php.ini
Scan for additional .ini files in: /etc/php.d
Additional .ini files parsed: /etc/php.d/curl.ini
...
3 change memory_limit in php.ini
vi /etc/php.ini
memory_limit = 512M
4 restart nginx and (php-fpm if being used)
service php-fpm restart
service nginx restart
If by increasing the memory limit you have gotten rid of the error and your code now works, you'll need to take measures to decrease that memory usage. Here are a few things you could do to decrease it:
If you're reading files, read them line-by-line instead of reading in the complete file into memory. Look at fgets and SplFileObject::fgets. Upgrade to a new version of PHP if you're using PHP 5.3. PHP 5.4 and 5.5 use much less memory.
Avoid loading large datasets into in an array. Instead, go for processing smaller subsets of the larger dataset and, if necessary, persist your data into a database to relieve memory use.
Try the latest version or minor version of a third-party library (1.9.3 vs. your 1.8.2, for instance) and use whichever is more stable. Sometimes newer versions of libraries are written more efficiently.
If you have an uncommon or unstable PHP extension, try upgrading it. It might have a memory leak.
If you're dealing with large files and you simply can't read it line-by-line, try breaking the file into many smaller files and process those individually. Disable PHP extensions that you don't need.
In the problem area, unset variables which contain large amounts of data and aren't required later in the code.
FROM: https://www.airpair.com/php/fatal-error-allowed-memory-size
This problem is happend because of php.ini defined limit was exided but have lot's of solution for this but simple one is to find your local servers folder and on that find the php folder and in that folder have php.ini file which have all declaration of these type setups. You just need to find one and change that value. But in this situation have one big problem once you change in your localhost file but what about server when you want to put your site on server it again produce same problem and you again follow the same for server. But you also know about .htaccess file this is one of the best and single place to do a lot's of things without changing core files. Like you change www routing, removing .php or other extentions from url or add one. Same like that you also difine default values of php.ini here like this - First you need to open the .htaccess file from your root directory or if you don't have this file so create one on root directory of your site and paste this code on it -
php_value upload_max_filesize 1000M
php_value post_max_size 99500M
php_value memory_limit 500M
php_value max_execution_time 300
after changes if you want to check the changes just run the php code
<?php phpinfo(); ?>
it will show you the php cofigrations all details. So you find your changes.
Note: for defining unlimited just add -1 like php_value memory_limit -1 It's not good and most of the time slow down your server. But if you like to be limit less then this one option is also fit for you. If after refresh your page changes will not reflect you must restart your local server once for changes.
Good Luck. Hope it will help. Want to download the .htaccess file click this.
来源:https://stackoverflow.com/questions/4096582/allowed-memory-size-of-x-bytes-exhausted