To find what’s consuming memory in a running PHP process, profile it with OpenResty XRay: it attaches to the unmodified process, ranks live PHP objects by the memory they hold, and traces the largest one back to its exact source line via a data reference path — no code changes and no restart.

This tutorial walks through that with OpenResty XRay’s guided analysis and the PHP object memory distribution flame graph we invented — in a real case, a single 40MB HTML string held by a controller.

Symptom: a PHP process using high memory in top

First, run the top command to check the memory usage. As shown, the php process uses the most memory — more than 37MB of RSS.

top output showing the php process using the most memory, over 37MB RSS

Let’s run the ps command for more details about this process. We can see it is the standard PHP binary executable shipped with the Linux distribution.

ps output showing the process is the standard /usr/bin/php binary

Let’s use OpenResty XRay to check out this unmodified process. We can analyze it in real time and figure out where exactly is the memory spent.

Profile the process with OpenResty XRay guided analysis to find the largest PHP objects

Open the OpenResty XRay web console and make sure you are watching the right machine. Go to the Guided Analysis page, where you can pick from the types of problems OpenResty XRay can diagnose.

OpenResty XRay Guided Analysis problem types, including High memory usage

Select High memory usage, choose the PHP application and the worker process that top flagged (the one using more than 35MB of RSS), keep both PHP and C/C++ language levels, and start. After a round or two of analysis, OpenResty XRay automatically produces a report. Under PHP-Land, it shows the PHP memory object reference path occupying the most memory.

The #1 hottest PHP GC object memory distribution reference path in the OpenResty XRay report

As you can see, the HTML source string of a web page takes up nearly 40MB of memory.

The web page HTML source string taking up nearly 40MB of memory

productPage is an attribute of the controller where that string is stored.

The productPage attribute of the controller holding the 40MB HTML string

The Illuminate\Routing\Route class maps URL requests to controller methods.

The Illuminate Routing Route class mapping URL requests to controller methods

This data reference path is automatically inferred from the PHP-land GC object memory distribution flame graph below.

PHP-Land PHP GC object memory distribution flame graph highlighting the hottest backtrace down to the 40MB string

Trace the largest object back to its source line

Copy the productPage property from the reference path, go to the PHP application’s source directory, and search for the files containing the productPage identifier with grep.

grep searching the source directory for the productPage identifier

This is the source line number of the assignment statement — line 40 of ProdController.php.

grep output showing the productPage assignment at ProdController.php line 40

Open the source file in your editor. This is where the property is defined.

The ProdController.php source file showing where the productPage property is defined

Go to line 40. The statement $this->productPage = file_get_contents($file_path) reads an entire page into memory in one shot, which creates the large memory object. It is recommended to use a streaming response to optimize memory usage.

ProdController.php line 40 assigning the whole file into productPage via file_get_contents

Monitor PHP process memory automatically with Insights reports

OpenResty XRay can also monitor online processes automatically and show analysis reports. On the Insights page you can find daily and weekly reports, so you don’t have to run the Guided Analysis manually — though Guided Analysis is still useful for application development and demonstration purposes.

OpenResty XRay Insights daily memory report for the PHP application

This walkthrough targets a large but stable memory footprint. If your PHP process instead keeps growing over time, that points to a leak — our guide explains how to tell a memory leak from high memory usage.

FAQ

How do I find which PHP process is using the most memory?

Run the top command — the php process shows up as the one using the most memory, more than 37MB in this case. Run ps for more details; here it’s the standard PHP binary shipped with the Linux distribution. OpenResty XRay then analyzes that exact process to find where the memory is spent.

Can I profile PHP memory usage without changing the code or restarting?

Yes. OpenResty XRay analyzes the unmodified, running PHP process in real time — in this case the standard PHP binary shipped with the Linux distribution. Because it works on the process as-is, you don’t modify or restart the application to see where its memory goes.

What is a PHP memory object reference path?

It’s the reference path to the object holding the most memory — here, the nearly 40MB HTML string stored in the productPage attribute of a controller, with the Route class mapping requests to that controller. OpenResty XRay infers this path automatically from the PHP-land GC object memory distribution flame graph.

How do I reduce high memory usage from a large PHP object?

Trace the largest object to the source line that creates it — here, a statement that assigns to the productPage property and creates a large memory object. OpenResty XRay’s report recommends using a streaming response to optimize memory usage.

What is OpenResty XRay

OpenResty XRay is a dynamic-tracing product that automatically analyzes your running applications to troubleshoot performance problems, behavioral issues, and security vulnerabilities with actionable suggestions. Under the hood, OpenResty XRay is powered by our Y language targeting various runtimes like Stap+, eBPF+, GDB, and ODB, depending on the contexts.

If you like this tutorial, please subscribe to this blog site and/or our YouTube channel. Thank you!

About The Author

Yichun Zhang (Github handle: agentzh), is the original creator of the OpenResty® open-source project and the CEO of OpenResty Inc..

Yichun is one of the earliest advocates and leaders of “open-source technology”. He worked at many internationally renowned tech companies, such as Cloudflare, Yahoo!. He is a pioneer of “edge computing”, “dynamic tracing” and “machine coding”, with over 22 years of programming and 16 years of open source experience. Yichun is well-known in the open-source space as the project leader of OpenResty®, adopted by more than 40 million global website domains.

OpenResty Inc., the enterprise software start-up founded by Yichun in 2017, has customers from some of the biggest companies in the world. Its flagship product, OpenResty XRay, is a non-invasive profiling and troubleshooting tool that significantly enhances and utilizes dynamic tracing technology. And its OpenResty Edge product is a powerful distributed traffic management and private CDN software product.

As an avid open-source contributor, Yichun has contributed more than a million lines of code to numerous open-source projects, including Linux kernel, Nginx, LuaJIT, GDB, SystemTap, LLVM, Perl, etc. He has also authored more than 60 open-source software libraries.