20 comments

[ 2.9 ms ] story [ 61.9 ms ] thread
fixing things like this often means going round the houses, nice to have a debugging checklist like this!
This 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.
It'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.

@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.
Setting 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).
Increasing 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.
Sorry I missed that section. Carry on!
Unless 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)
Being 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?
Good call. Minor oversight. Just updated that. I think if your NOT a hardcore technologist you probably think of megabytes as as 1000^n. It's really easy to forget too for the rest of us.
Über-pedantry: It's "you're" and not "your".
Unfortunately, HN doesn't have an edit function for comments. ;)
It does, but only for comments that haven't been replied to
More pedantry ;)
I was actually trying to be helpful there :(
Hehe. Based on the previous pendantry, it blended in. I'll take it as help then. :D
The previous pedantry wasn't me though; only the MiB / MB comment. And that was partly out of curiosity if 1000MB == 1GB was a deliberate rounding.
"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.
I 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:"