HomePHPHow to test php memory limit

How to test php memory limit

memory_limit sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts from eating up all available memory on a server.

There are many ways by which you can set the memory limit.

1. Set memory_limit = 16M to your server’s php.ini file (recommended, if you have root access) [applies to the whole server]

2. Set ini_set(‘memory_limit’, ’16M’); to your site’s default settings file [in case of some CMS sites] [applies to the whole site]

3. Set php_value memory_limit 16M to your .htaccess file in the site’s folder [applies to the scripts under that folder].

To check the memory limit applicable in the server [or in a particular folder], create a filememtest.php in the desired location, with the below contents.

 <?php
 $step = 1;
 while(TRUE) {
  $chunk = str_repeat('0123456789', 128*1024*$step++);
  print 'Memory usage: '. round(memory_get_usage()/(1024*1024)) . 'M<br />';
  unset($chunk);
 }
 ?>

Now access the file in your browser. The script executes till it reaches the limit.

Scroll to Top