When adding Octane to an existing @laravelphp app, spotting memory leaks can be quite tricky. πŸ”Ž

This thread lists some basic techniques that helped me out, hopefully it can be useful to other devs. 🧡

#LaravelOctane #laravel #php
@laravelphp During development, make sure to display/log memory usage peaks in all your pages/scripts.

If the peak keeps increasing esponentially at every refresh, there is a memory leak in the current page/script. 🐘
@laravelphp As suggested by the Laravel team, the first thing to do is checking for singleton bindings in all service providers (yours and 3rd party packages').

Singletons shouldn't inject or call instances of Application, Request or config Repository classes. ⚠️
@laravelphp If 3rd party service providers register singletons that provoke memory leaks, you can try to remove them from the container before the next request.

This is possible by listing the affected bindings in the `flush` array in config/octane.php 🌊
@laravelphp Sometimes flushing singletons may not be enough, e.g. when stale instances of Application are used internally by packages.

In those cases, packages need some changes. Consider sending a PR if you know how to fix memory leaks of a package. 🌈
@laravelphp If the package causing memory leaks is not essential for your app, you can temporarily disable it by avoiding its discovery.

Remember to delete bootstrap/cache/services.php and bootstrap/cache/packages.php when enabling/disabling packages. πŸ‘ˆ
@laravelphp No clue of what is going on? A good starting point can be commenting out custom service providers in config/app.php and avoiding the discovery of all installed packages.

If the memory peak stops increasing, re-enable service providers one-by-one to find the culprit(s). πŸ•΅οΈ
@laravelphp If you are using @LaravelLivewire, avoid to call the `@livewire()` directive within a view already rendered by a Livewire component.

That was one of my mistakes that caused a memory leak πŸ€¦β€β™‚οΈ
@laravelphp @LaravelLivewire @livewire Another mistake I made was creating a new Collection while grouping items of a paginator.

If you need to manipulate paginated results, make sure to replace the paginator collection with the manipulated data. ☒️
@laravelphp @LaravelLivewire @livewire Pay special attention to static properties and static variables, especially if they are arrays or ArrayAccess objects.

If they are not properly cleared, values may be added to them until the memory is exhausted. 🧐
@laravelphp @LaravelLivewire @livewire Also, consider using Larastan to spot irregular bindings: github.com/nunomaduro/lar…

If you ran into other memory leaks, I would be curious to know more about how you found/fixed them. πŸ€™

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Andrea Marco Sartori

Andrea Marco Sartori Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(