How to Check Memory Usage by PHP Script

Have you ever checked, How much memory your PHP script takes when it executes.  In this tutorial, We’ll learn how to check memory usage by PHP script.

To check memory usage, PHP provides an inbuilt function memory_get_usage(). This function returns the amount of memory allocated to a PHP script.

** memory_get_usage() function returns the amount of memory allocated to your PHP script in bytes.

To convert them in KB – memory_get_usage/1024

To convert them in MB – memory_get_usage/1048576


Subscribe Our Tutorials

Get Latest Updates on Facebook

PHP Built-in Web-Server

How to Check Memory Usage by PHP Script

Let’s demonstrate this through an example.

Here is a simple PHP script, which creates an instance of MongoDB.

It is very useful to know how much memory is allocated during your PHP script execution so that you can avoid Fatal Error: Allowed memory size exhausted. Manual


About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , . Bookmark the permalink.
  • take a look at “Xdebug + Profiling” it can’t profile memory but it’s very helpful for debugging “performance issues :)

    1.) install “Xdebug”: (

    sudo apt-get install php5-xdebug

    2.) configure “Xdebug”:

    xdebug.profiler_enable_trigger = 1
    xdebug.profiler_output_dir = “/tmp”
    xdebug.profiler_output_name = “cachegrind.out.%t-%s”
    xdebug.remote_enable = 1
    xdebug.remote_handler = “dbgp”
    xdebug.remote_host = “”
    xdebug.trace_output_dir = “/tmp”


    send “XDEBUG_PROFILE=1″ via _GET or _POST

    4.) use e.g. “QCacheGrind”, “WinCacheGrind” or “KCachegrind” ( to visualize the results

    PS: and there is also a memory usage profiler for the “Cachegrind”-Tools -> “memprof”(, but in the most cases XDebug will help you :)