Oh I don't load hundreds of models, it's a little app with three major models, only one ot two users, and some other models (about 10) for the caracteristics (10 to 15 rows used in select option for each caracteristic) of the three models.
There will be a document management for two models.
So I will have some 15 to 20 tables max, so 15 to 20 models.
@vincent15000 Late reply, but thats not how models loading works. if your users table has 1000 users, and you do users::all(), it will load 1000 models. and if those users have a relationship eager loaded say user has a designatination. then it will load 1000 designation models as well, so total 2000 models will be loaded at once. if you do pagination of 10 records per page, it will load 20 models. user+designation. All the models consume memory, as a rough estimate 0.2MB per models. for 2000 that's 2000*0.2 = 400MB of ram, plus laravel and stuff +- 5MB extra.
And that's for a single request, if say 10 requets hit this page at once, it will cause 400*10 = 4000MB memory utilization, which of course would pre-maturely cause memory exhaust error after a couple of requests until all requests are finished i.e. requests are stopped or reduced
"Oh I don't load hundreds of models, it's a little app with three major models, only one ot two users
So I will have some 15 to 20 tables max, so 15 to 20 models."
Maybe I misunderstood, but from this text, it seems he is mixing loaded eloquent models with the Model class. doesn't matter if he has Only 1 Model/Table, that single table could have millions of rows. And fetching huge number of rows would consume memory causing a memory_limit hit.
It can be one single call to Modell::all() or multiple concurrent users making requests in short bursts.
Hence my reply. Overall I agree with your point, Cheers
if you are making a website where very limited number of users, then it will be fine. but consider at least 512 MB Limit which have no much price difference.
I did'nt know this function memory_get_usage(). It's ok for me. It seems I don't need more than 10M PHP memory. So 256M is ok for me ;). Thanks @snapey and @sr57.
memory_limit is to limit the max memory allocation to the php process, your server physical RAM should much more than this.
probably just let it as default, until you hit issue then only fine tune... but try to understand why it need to increase the limit and also the impact before increase,sometimes fine tune the query or logic may help also...
@vincent15000 You can use a dev tools like barryvdh/laravel-debugbar package. It shows a debug bar at the bottom of every page with stats like memory usage to load the page, queries that you can inspect for potential n+1 problems, etc. Very useful.