Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the memory available to PHP

I'm currently using the memory_get_usage function to determine how much memory my PHP script is using. However, I want to display the memory usage value against the total amount of memory available to PHP.

How to I get the total amount of memory available to PHP?

like image 944
Luke Avatar asked Dec 29 '13 22:12

Luke


2 Answers

You can use the php.ini setting memory_limit:

ini_get('memory_limit');

Technically, this is not the total amount available to PHP, it's the amount available to a PHP script.

like image 57
ComFreek Avatar answered Sep 26 '22 20:09

ComFreek


There is a problem that by using ini_get you'll get just the string from php.ini (e.g. 500 or 500M or 1G etc.).

Should you need the real value in bytes, you can use this simple open source tool: https://github.com/BrandEmbassy/php-memory

Then you can just do this: $limitProvider->getLimitInBytes(); and the library converts all valid values for you automatically.

like image 24
Ivan Kvasnica Avatar answered Sep 23 '22 20:09

Ivan Kvasnica