How to check how much memory your PHP scripts use: a tutorial with examples

21 thoughts on “How to check how much memory your PHP scripts use: a tutorial with examples”

  1. Hi Alex,
    My script utilizes just 3-4MB but making 2 simultaneous requests would return error 504 Resource Limit even though the memory limit is set to 128MB.
    Any help what could cause this or a walkaround?

    • Hello Richard,
      Are you getting an 504 or 508 error? I’m asking because Resource Limit has the 508 code.

      Anyway, there can be other issues other than memory, such as the execution time. On top of that, your hosting may enforce stricter memory limits then the ones set by PHP.

      If you want, you can share the code using Pastebin and I’ll take a look. For a quicker reply you can also post it on my Facebook group:

  2. If the real_usage argument is set to true the PHP DOCS say it will get the real size of memory allocated from system. If it’s false it will get the memory reported by emalloc()

    Which one of these 2 options returns the max. memory allocated relative to the memory limit value in php.ini ?

    I want to know how close was the script to hit that limit.

    • The description of the “memory_limit” parameter says:
      “This sets the maximum amount of memory in bytes that a script is allowed to allocate.”

      This suggests that the PHP limit is based on the *allocated* memory and not on just the used memory. Therefore, you need to set the real_usage parameter to TRUE to see how close to the limit it is.

  3. The below method could be help you:

    function getVariableUsage($var) {
    $total_memory = memory_get_usage();
    $tmp = unserialize(serialize($var));
    return memory_get_usage() – $total_memory;

    $var = “Hey, what’s you doing?”;
    echo getVariableUsage($var);

  4. Hi Alex, I want to implement the email sending code in laravel since our website is getting too many request. I am confused where to put this php code, should I create a new middleware for this? sorry I am new to laravel.

        • Thanks it looks really nice, it’s been a while… 😉

          BTW I was thinking that an important check to do could be to calculate available memory like as:


          There are some situations where the available RAM can be less than memory_limit (for example multiple daemons running on a shared server).

          I don’t know what overhead would be added by checking free RAM, though. But it’s still a possibility. Maybe only at start time to skip if there is low ram…

          Also I was wondering if memory used by resources is computed in PHP’s own memory_limit enforcing and what memory_usage value is used (allocated or used) for that.

          Ps. I use a Telegram Bot to send important log messages to myself instead of emails.

          I’d love to read your opinions.

    • Hi (I’ll answer here to avoid too much nesting).

      It may happen that the free system memory is less than the PHP memory limit, though such scenario would very much likely mean something’s wrong with the server itself (i.e., way too much load or memory leaks somewhere).

      So yes, checking the free ram before running the script core can be a good idea if the server has a very low amount of RAM. The check should be done at the beginning of the script in run-once scripts, and at the beginning of every iteration in PHP daemons.

      To answer your remaining questions:
      – the RAM check itself has no significant overhead
      – resources are not included into both memory_limit and memory_usage, but you can use some system tools to get their memory usage (see the post for the details)

      Thanks again for your feedback!


Leave a Comment