Approaches to track memory leaks in async php #166
Replies: 2 comments 7 replies
-
@maciejfikus Thank for reporting, this is an interesting one! To the best of our knowledge, Framework X does not exhibit any memory leaks on its own and can safely run for months straight. PHP has a deterministic garbage collector which should make it "virtually impossible" to leak memory in unpredictable places. That said, it's definitely possible to "waste" memory depending on how your application logic works. For instance, if you keep adding an element to an array for each incoming request, you will see that memory usage keep growing for each additional HTTP request. X is carefully designed to avoid any unneeded memory allocations, but depending on your application logic, this memoization strategy is definitely something we've seen in the wild and should be avoided for long-running application servers. If you think you've found a memory leak in any of the underlying components, reporting this upstream is definitely the way to go. I see you've reported this upstream in PgAsync. We don't maintain this component, but I'm also not aware of any known memory leaks here. I did take a quick look at your ticket and don't see anything wrong after eyeballing this. If you can pinpoint this to a single component, creating a simple reproducer definitely helps. Perhaps you can work out a set of commands to reproduce this locally (such as starting a blank database in a container and a simple gist to reproduce this)? I hope this helps 👍 |
Beta Was this translation helpful? Give feedback.
-
@clue, In fact, I've gotten to the point where I believe FrameworkX also contributed to the memory problem. Otherwise, this is normal GC behavior in PHP when using fibers. In the last example I posted here even though the memory is leaking (as I mentioned in the thread), manually running the garbage collector in a loop resets the reserved memory to its normal state.
The same workaround will not work when using FrameworkX.
I believe that PgAsync is not working properly, but at this point I am wondering why GC is not able to recover memory as soon as FrameworkX and its routing joins the party? |
Beta Was this translation helpful? Give feedback.
-
As I am having really tough time with debugging my already production-deployed app I wonder how do you track memory issues in async php?
I mean, it took me a few hours by trial and error to figure out what part of my app is responsible for memory leak. I wasn't sure if it is X itself, my own code or some package i.e. PgAsync. By now I am 99% sure it is fault of PgAsync (voryx/PgAsync#48) but after all I am not able to locate exact place and reason there.
I tried to debug it with php-memprof, but it doesn't say much to me, not sure if it is suitable for async envs tho. What approaches and tools do you use to find out stuff like that?
Beta Was this translation helpful? Give feedback.
All reactions