Taming your PHP app's memory footprint

Learn best practices for memory management in PHP applications, including common causes of high memory usage and techniques to reduce memory footprint.

Whenever you run a PHP application or any other software, it consumes memory. Basically, all applications require memory to run. With all these apps competing for resources, the computer has to allocate memory efficiently. This brings us to the memory management process. What is it?

Memory management involves freeing, allocating, organizing, and optimizing available computer memory to ensure programs run well. Most of the time, the computer does memory management automatically, but as a PHP developer, you have a duty to manage your app's memory footprint.

Join us in this article as we discuss the techniques you can use to lower your PHP application's memory footprint.

Why is memory management important?

PHP applications usually have a memory limit of about 128 MB. If your application surpasses this limit, you will experience the following fatal error:

Fatal error: Allowed memory size of 94371840 bytes exhausted (tried to allocate 5072 bytes)

The above error will cause your application to crash, which can be quite frustrating. In certain cases, you may experience lags and freezes, ultimately affecting the overall user experience.

Although you can extend your PHP app's memory limit, it can be a long and complicated process that should only be the last resort. A good alternative is reviewing your application and correcting areas that may result in high memory consumption.

You can find the amount of memory your PHP application consumes and its peak usage using the following methods:

// Current memory usage
 $memory_usage = memory_get_usage();

// Peak memory usage
$memory_peak = memory_get_peak_usage();

Next, let's look at some common causes of high memory usage in PHP applications.

Common causes of high memory usage

Here are some common causes of high memory usage in PHP applications.

Memory leaks

A memory leak happens when developers allocate a certain percentage of memory to their application but forget to free it once a particular task is complete.

If your application is running on a local server, you will notice a decrease in performance, especially if you are running a time-intensive task. This is because the available memory is quite small, and the computer must still allocate it to other applications.

PHP libraries and extensions

PHP libraries are important because they allow us to add functionality to our project without writing much code. However, overlying on PHP libraries has its drawbacks. For instance, some extensions may end up consuming more memory. The worse thing is that you have little control over them since the code is not open-source.

Failure to update the PHP version

In some cases, your PHP application may consume more memory simply because you are using an old version of PHP. Such versions may have bugs or inefficient code, impacting your app's performance.

Running too many scripts

Websites executing too many scripts concurrently will record high memory usage, especially if they contain inefficient code. Some PHP scripts feature endless loops or timers, meaning they will keep running for a long time. There's a high likelihood of such websites crashing. Additionally, noticeable lags in the user interface will cause a bad user experience.

Now that we know the common causes of high memory consumption in PHP applications, let's proceed and discuss how to reduce your app's memory usage in the following section.

How to reduce your PHP app's memory footprint

As you create or deploy your PHP application, here are tips to reduce its memory footprint.

Fix memory leaks

A PHP app can consume more memory simply because certain sections of your code are still running and consuming resources, yet they're no longer required. As your application grows, such sections can slow performance and cause fatal crashes due to high memory usage.

The first step in fixing memory leaks is to identify which scripts are consuming exceptionally high memory than normal. You can generate a log of your scripts using the following code.


Once the logs are generated, review them and determine which ones consume more memory.

You can also determine the exact amount of memory the scripts consume using the following methods:

//Currently used memory

//Peak memory usage

For example, the following script to calculate the Fibonacci sequence only consumes 389KB of memory. However, note that the amount of memory may increase as the Fibonacci sequence grows.


function fibonacci($n) {
    global $previousValues;
  if(array_key_exists($n, $previousValues) ){
    return $previousValues[$n];
  else {
    if($n > 1) {
      $result = fibonacci($n-1) + fibonacci($n-2);
      $previousValues[$n] = $result;
      return $result;
    return $n;

$number = 9;
echo fibonacci($number);
/* Displaying used memory */
echo '<br>Memory consumed: ' . round(memory_get_usage() / 1024) . 'KB<br>';
/* Displaing peak memory usage */
echo 'Peak usage: ' . round(memory_get_peak_usage() / 1024) . 'KB of memory.<br><br>';

When you determine that a script is indeed consuming more memory, go through the code and try to identify the responsible sections (memory leaks). You can comment out certain sections of your code (that you suspect to be memory-intensive) and execute the code again. Optimize the functions responsible for the high memory leaks and run your tests again.

Reading files and database records

In some PHP applications, you may need to read, create, modify, and delete files. Although these operations are important, they may lead to high memory usage in your application, especially when incorrectly implemented.

One tip to save memory while handling large files is to read their contents line by line rather than saving the whole file in memory. Let's see why.

The file_get_contents method is used to retrieve the contents of a file and store it in a string. This method is executed quickly when a small file is involved, but as the size grows, your application may require more memory and finally crash when the limit is reached.

Here is an illustration of the file_get_contents method:

//loading a large text file (1GB) using the file_get_contents method
$myfile = file_get_contents('myverylargefile.txt');
echo $myfile;

When you run the above code, you will experience the following error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 529084788 bytes) in index.php

The above error occurred because PHP attempted to load a file of about 1GB in memory, but the allowed limit for the script is about 128MB.

We can avoid the above error and lower our memory foot my simply reading large files line-by-line using fopen, fgets, and feof methods, as demonstrated below.

//File path.
$file = 'myverylargefile.log';

//Opening the file in "reading only" mode.
$fileHandler = fopen($file, "r");

//We throw an exception in case if we do find the file.
if($fileHandler === false){
    throw new Exception('Cannot find: ' . $file);

//We use a while loop to go through the file
while(!feof($fileHandler)) {
    //We read the current line
    $line = fgets($fileHandler);
    //We can save line in an array or run any operation.

//Close the file handler when done to avoid file corruption

The same principle applies when retrieving records from a database. Rather than fetching all entries at once, loop through the database and retrieve records individually.

In the following example, about 642kbs of memory will be consumed when all data is retrieved from the database simultaneously:

// Fetching all records at once
$results = $stmt->fetchAll();
foreach ($results as $r) { print_r($r); }


PEAK USAGE - 642kbs

In contrast, 626kbs of memory will be consumed when you fetch records individually from the database using the following code. Although it may seem minor, it can give your application a huge performance boost, especially when lots of data are involved.

while ($r = $databasestatement->fetch()) { 


PEAK USAGE - 626kbs

Here is the entire code for retrieving MySQL database records. Note that the inefficient code for retrieving all database records at once has been commented out.

//Database connection details
$db_host = "localhost";
$db_name = "products";
$db_charset = "utf8mb4";
$db_user = "root";
$db_password = "";

// Establishing a database connection
$conn = new PDO(
  $db_user, $db_password, [

// Preparing SQL statements
$databasestatement = $conn->prepare("SELECT * FROM `products`");

// Unrecommended method - Fetching all records 
// $results = $stmt->fetchAll();
// foreach ($results as $r) { print_r($r); }

// Preferred method - Fetch records line by line
while ($r = $databasestatement->fetch()) { 

// Once done retrieving records, close the database connection
if ($databasestatement !== null) { $databasestatement = null; }
if ($conn !== null) { $conn = null; }

echo "PEAK USAGE - " .  round(memory_get_peak_usage() / 1024). "kbs" ;

Use the latest PHP version

Always ensure that you are using the latest version of PHP to build applications. The latest version not only makes your website more memory-efficient but also improves overall security by resolving bugs.

If you have an old website, you can upgrade your existing codebase to use the latest PHP version.

Disable unused plugins

As mentioned, over-reliance on PHP libraries may cause your app to consume more memory. This is because you have little control over them. Therefore, it’s recommended to go through your project and identify and disable plugins you don't need.

You should also research and find out what other developers say about different plugins before incorporating them into your project.


Lowering your PHP app's memory usage can facilitate a better user experience. Not only will your scripts and web pages load faster, but also the likelihood of the website crashing, freezing, or lagging is low.

Reduce your app's memory footprint today by finding and eliminating memory leaks, reading files effectively, and disabling unused scripts and libraries.

What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo

    Michael Barasa

    Michael Barasa is a software developer. He loves technical writing, contributing to open source projects, and creating learning material for aspiring software engineers.

    More articles by Michael Barasa
    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "We've looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release."
    — Michael Smith, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like:

    “Everyone is in love with Honeybadger ... the UI is spot on.” 
    Molly Struve, Sr. Site Reliability Engineer, Netflix
    Start free trial
    Are you using Sentry, Rollbar, Bugsnag, or Airbrake for your monitoring? Honeybadger includes error tracking with a whole suite of amazing monitoring tools — all for probably less than you're paying now. Discover why so many companies are switching to Honeybadger here.
    Start free trial
    Stop digging through chat logs to find the bug-fix someone mentioned last month. Honeybadger's built-in issue tracker keeps discussion central to each error, so that if it pops up again you'll be able to pick up right where you left off.
    Start free trial