Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Laravel and PHP memory_limit

Hello,

I have to choose a web host for my app.

Some web host limit the PHP memory_limit to 256M.

How is it possible to estimate the PHP script memory for a Laravel app ?

Thanks for your answer.

Vincent

0 likes
16 replies
Snapey's avatar

Really hard to guess because it depends a) how efficient your code is and b) how many concurrent users you realistically expect.

If you load hundreds of models into memory then you might start to be challenged on Ram, but for most sites, 256MB will be fine.

You will see VPS servers with memory mentioned in terms of 1 or 2 GB but this ram has to hold the entire OS and the SQL database as well as php

1 like
vincent15000's avatar

Thank you @snapey

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's avatar

Ok ... but I'm talking about PHP memory_limit and not RAM ... is it the same ? I don't think so.

memory_limit is the limit size for a PHP script.

bsienn's avatar

@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

1 like
Snapey's avatar

@bsienn thats why you never load 1000 models into memory. And not really relevant to the question.

1 like
bsienn's avatar

@Snapey He said

"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

RahulKmOfficial's avatar

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.

1 like
sr57's avatar
sr57
Best Answer
Level 39

For small app php memory is rarely a pb and 256M is enough.

Most of the time people let this at this value and adjust it (or review the code) when pb arrives.

If you want to be more pro-active you can follow php memory usage with these 2 functions memory_get_usage() and memory_get_peak_usage().

You can do a real time monitoring by logging these values or just have one shot review to see what you are using now.

1 like
vincent15000's avatar

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.

siangboon's avatar

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...

1 like
Merklin's avatar

@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.

1 like

Please or to participate in this conversation.