In this tutorial, you will learn how to quickly identify the largest PHP memory objects or values in your applications. Thanks to the power of OpenResty XRay; we can pinpoint these PHP memory objects via detailed data reference paths. We’ll also demonstrate the PHP object memory distribution flame graphs invented by ourselves. Memory leaks or large memory footprints are no longer a myth in your PHP code!

Problem: high memory usage

First, run the top command to check the memory usage. As shown, the php process uses the most memory.

Screenshot

It’s consuming more than 37MB of the memory.

Screenshot

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.

Screenshot

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.

Use the guided analysis feature of OpenResty XRay to find the largest Perl objects or values taking the most RAM

Open the OpenResty XRay web console in the web browser. Make sure it is the right machine you are watching.

Screenshot

You can change machine from the list below.

Screenshot

Go to the “Guided Analysis” page.

Screenshot

Here, you can see different types of problems that you can diagnose.

Screenshot

Let’s select “High memory usage”.

Screenshot

Click on “Next”.

Screenshot

Select the PHP application.

Screenshot

Select the process that consumes more than 35MB of the memory. This is what we saw previously in top.

Screenshot

Make sure that the application type is right. Usually, the default should be correct.

Screenshot

OpenResty XRay can analyze multiple language levels at the same time. We’ll keep both PHP and C/C++ selected.

Screenshot

We can also set the maximum analyzing time. We’ll leave it as 300 seconds, which is the default value.

Screenshot

Let’s start analyzing.

Screenshot

The system will keep performing different rounds of analysis. Now, it’s executing the first round.

Screenshot

The first round is done, and it’s on to the second one already. That’s enough for this case.

Screenshot

Let’s stop analyzing.

Screenshot

We can see it automatically creates an analysis report.

Screenshot

This is the type of problem we are going to diagnose. It’s memory.

Screenshot

This is the PHP memory object reference path occupying the most memory.

Screenshot

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

Screenshot

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

Screenshot

The main function of the Route class, is to map URL requests to controller methods.

Screenshot

Click on “More”.

Screenshot

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

Screenshot

Below are more detailed explanations and suggestions regarding the current issue.

Screenshot

Let’s go back to the original PHP data reference path. Copy productPage property.

Screenshot

Go to the PHP application’s source directory. Search for the files containing the productPage variable identifier via grep command.

Screenshot

This is the source line number of the assignment statement.

Screenshot

Copy the name of the source file. Use the vim editor to open the source file. You can use any editor you like.

Screenshot

Here is where the property is defined.

Screenshot

Go to line 40.

This statement assigns a value to the productPage property. This will create a large memory object. It is recommended to use streaming response to optimize memory usage.

Screenshot

Automatic analysis and reports

OpenResty XRay can also monitor online processes automatically and show analysis reports. Go to the “Insights” page.

Screenshot

You can find the reports on the Insights page for daily and weekly periods. For this reason, you don’t have to use the “Guided Analysis” feature.

Screenshot

Though “Guided analysis” is useful for application development and demonstration purposes.

Screenshot

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.