This error message is being presented, any suggestions?
Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php
This error message is being presented, any suggestions?
Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php
If your script is expected to allocate that big amount of memory, then you can increase the memory limit by adding this line to your php file
ini_set('memory_limit', '44M');
where 44M is the amount you expect to be consumed.
However, most of time this error message means that the script is doing something wrong and increasing the memory limit will just result in the same error message with different numbers.
Therefore, instead of increasing the memory limit you must rewrite the code so it won't allocate that much memory. For example, processing large amounts of data in smaller chunks, unsetting variables that hold large values but not needed anymore, etc.
Your script is using too much memory. This can often happen in PHP if you have a loop that has run out of control and you are creating objects or adding to arrays on each pass of the loop.
Check for infinite loops.
If that isn't the problem, try and help out PHP by destroying objects that you are finished with by setting them to null. eg. $OldVar = null;
Check the code where the error actually happens as well. Would you expect that line to be allocating a massive amount of memory? If not, try and figure out what has gone wrong...
I have faced same problem in php7.2 with laravel 5.6. I just increase the amount of variable memory_limit = 128M in php.ini as my applications demand. It might be 256M/512M/1048M.....Now it works fine.
It is unfortunately easy to program in PHP in a way that consumes memory faster than you realise. Copying strings, arrays and objects instead of using references will do it, though PHP 5 is supposed to do this more automatically than in PHP 4. But dealing with your data set in entirety over several steps is also wasteful compared to processing the smallest logical unit at a time. The classic example is working with large resultsets from a database: most programmers fetch the entire resultset into an array and then loop over it one or more times with foreach(). It is much more memory efficient to use a while() loop to fetch and process one row at a time. The same thing applies to processing a file.
If you want to read large files, you should read them bit by bit instead of reading them at once.
It’s simple math: If you read a 1 MB large file at once, than at least 1 MB of memory is needed at the same time to hold the data.
So you should read them bit by bit using fopen & fread.
If you are trying to read a file, that will take up memory in PHP. For instance, if you are trying to open up and read an MP3 file ( like, say, $data = file("http://mydomain.com/path/sample.mp3" ) it is going to pull it all into memory.
As Nelson suggests, you can work to increase your maximum memory limit if you actually need to be using this much memory.
We had a similar situation and we tried out given at the top of the answers ini_set('memory_limit', '-1'); and everything worked fine, compressed images files greater than 1MB to KBs.
If you are using a shared hosting, you will not be able to enforce the increment in the php size limit.
Just go to your cpanel and upgrade your php version to 7.1 and above then you are good to go.
I want to share my experience on this issue!
Suppose you have a class A and class B.
class A {
protected $userB;
public function __construct() {
$this->userB = new B();
}
}
class B {
protected $userA;
public function __construct() {
$this->userA = new A();
}
}
this will initiate a chain formation of objects which may be create this kind of issue!
I ran the following command composer update, and now are working. If its not solve your problem increase your memory limit in memory_limit in the php.ini file.
I had the same issue which running php in command line. Recently, I had changes the php.ini file and did a mistake while changing the php.ini
This is for php7.0
path to php.ini where I made mistake:
/etc/php/7.0/cli/php.ini
I had set memory_limit = 256 (which means 256 bytes)
instead of memory_limit = 256M (which means 256 Mega bytes).
; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 128M
Once I corrected it, my process started running fine.
If you are using Laravel, then use these ways:
public function getClientsListApi(Request $request){
print_r($request->all()); //for all request
print_r($request->name); //for all name
}
instead of
public function getClientsListApi(Request $request){
print_r($request); // it show error as above mention
}
wordpress users add line:
@ini_set('memory_limit', '-1');
or
set memory_limit to -1 in cpanel