r/PHPhelp • u/deliciousleopard • Nov 19 '25
Debugging memory usage in dev vs prod
I've got a weird memory usage issue where my site is using about 2x as much memory on my managed stage and prod servers compared to my local dev environment. Are there any readily available tools to debug situations like this? Preferably I'd just want a human readable heap dump from each environment so that I can compare what's being allocated.
When googling all I can find are some old non-standard extensions and various paid APMs.
4
Upvotes
5
u/PrizeSyntax Nov 19 '25
Check the database size. If you do sme big selects and the prod database is bigger that means more memory will be used. Another memory hog is pdf creation usually. Exports too, if you create CSV files, Excel files etc
Edit: other than that, you can play around with memory_get_usage or memory_get_peak_usage
3
u/thewallacio Nov 19 '25
When you say your "site" is using 2x memory, what exactly do you mean by that? I know that sounds like an obtuse question but I'd like to know which metric you are using to compare. It could be any number of PHP-FPM processes, your HTTP server, database server that are consuming memory so a level of detail here would be helpful.
I would imagine that at least your production server is seeing more traffic than your local dev environment so higher resource use across the board is to be expected.
We used to use Blackfire for a deep-dive into the PHP side of things but they removed the free tier so haven't used it in a while. Basic monitoring of processes using top/htop (if it's an *nix server) might give you some insight too.
3
u/Ahabraham Nov 19 '25
https://github.com/longxinH/xhprof Is good albeit annoying to setup. We use it in prod with hidden headers or query parameters to enable one off profiling. https://github.com/adsr/phpspy Can also be good and needs no application changes, and more critically can hook into running processes.
1
u/isoAntti Nov 19 '25
Any data objects you get from outside or database, might be stored multiple times on the way in. Dunno if you can really do anything about it.
1
u/Grouchy_Brain_1641 Nov 19 '25
Just htop and the New Relic APM for me.
1
u/shamunrr Nov 20 '25
XHProf with XHGui is good for being free. It is limited in that it's not exactly friendly.
I would highly recommend Blackfire.io however. It tracks database calls and network requests as well.
1
u/ArthurOnCode Nov 21 '25
Composer may be generating a different autoloader for production. Classmaps trade memory use for performance, but are annoying during development.