6 ms·
How to Fix PHP's Most Annoying Error
- stevejpurves 12y agofixing things like this often means going round the houses, nice to have a debugging checklist like this!
- trebor 12y agoThis should be titled "how to find a memory leak". It's a very basic article on memory management and how to find a memory leak.
- laumars 12y agoIt's not technically a memory leak though. It's more to do with finding errors in your code that use unexpectedly large quantities of memory. But since everything is being allocated and deallocated correctly, you're not actually leaking any memory (which I guess you could argue, makes the article even more basic). In any case, you're right that the title is a little misleading, but pedantry aside, it's still a useful resource.
- 2upmedia 12y ago@trebor: yes, just like @laumars mentioned this article talks very little about memory leaks. Ever since PHP 5.3 the Garbage Collector was introduced which took care of the biggest contributor to memory leaks: circular references in objects. This article talks more about reducing high memory usage which can happen in any version of PHP.
- martin_ 12y agoSetting a 1gb memory limit is a great way to hide the problem and dramatically reduce the maximum number of concurrent requests to your website, and drain resources from any other services running on the box(es).
- 2upmedia 12y agoIncreasing the memory limit is only temporary. It's used as a debugging procedure. I later write "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" with a list of things to do to decrease memory usage.
- martin_ 12y agoSorry I missed that section. Carry on!
- arenaninja 12y agoUnless you need the limit to be that high. But I agree that it isn't ideal. However, PHP cleans up nicely once the process ends, and manually freeing memory space can slow PHP down. The only exception I would take to this, I believe, are resources. If is_resource($foo) returns true, you should probably free it (looking at you, JpGraph)
- laumars 12y agoBeing massively pedantic for a moment; he didn't set the memory limit to 1GB but instead to 0.9765625GB: <?php ini_set('memory_limit', '1000M'); // or you could use 1G ?> Joking aside, do most people these days just use MB (1000^n) instead of MiB (1024^n) when working with RAM? Is this now the standard way to calculating memory?
- serve_yay 12y agoGood lord.
- lazyant 12y ago"Your first course of action is to increase your memory limit" - No. First thing you do is baseline your server and figure out how much max memory you want to allocate to PHP given the maximum that can be used from the other processes. OP's advice is a recipe for oom'ing your VPS. Also if there are more developers, hiding the memory allocation in code instead of setting file is going to be fun.
- 2upmedia 12y agoI mention: "never do these tests on a production server unless you’re sure you have plenty of RAM and you fully understand how web server processes consume memory. You could easily bring a server to its knees if there are many concurrent processes running, each using a high amount of memory." It's a temporary debugging procedure. The obvious goal to reduce memory usage. Later on I say: "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:"