Jump to content

Solution to Fatal error: Allowed memory size of 67108864 bytes exhausted


redfort

Recommended Posts

Hi ,

 

As you may all have experienced this problem is there way we can non-healthy exit of script ..due to memory problem........???

 

I got to know..we avoid this......by properly managing our php memory by unset function. remove unusable variables and objects from memory......and increasing memory limit in phph.ini

 

Is there any way we can check each time inside the script -to check memory is available or sleep for some time and check again.......to proceed without exit........

 

Is there a way to catch this fatal error and take necessary steps to avoid exit........

 

Thanks,

redfort

Link to comment
Share on other sites

There is a function that allows you to check the currently allocated memory:

 

http://php.net/manual/en/function.memory-get-usage.php

 

However, you cannot spawn a background task to control it, because PHP scripts are not multithreaded. Asking PHP for this 1000 times during the execution is also a bad idea. You cannot catch this kind of fatal error, too - how do you think it should work, if the memory is already exhausted?

 

If your script requires so much memory, track it with XDebug and simply try to optimize it. Learn some memory-saving techniques, better algorithms, how does garbage collection in PHP work, etc.

Link to comment
Share on other sites

  • 4 weeks later...

As has already been posted, the first step is to try to understand how your code is using memory and try to optimize accordingly, and it sounds like you're already doing a good bit of this. However much you optimize, one of the key goals you should try to achieve is using memory predictably. Once you can accurately predict approximately how much memory your code will require, you can adjust the server configuration (physical RAM and PHP max allocation in php.ini) as needed.

 

One other thing to keep in mind is that there's a line between optimizing code and increasing physical resources, and where the line is will depend on how important the app is to your company, how much money you have available to spend on the server(s), how valuable developer time is compared to the costs of hardware and associated maintenance, and so forth. Obviously, every situation puts the line in a unique spot. But, don't lose site of this and, for example, spend a ton of money on time trying to optimize for additional milliseconds when you can spend a small amount simply adding RAM to the server, or too much money throwing hardware at a problem easily solved in software.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.