@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 π€¦ββοΈ
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. π§