PHP ini_set Memory Limit

Sarwan Soomro Jan 30, 2023
  1. Set Memory Limit Using PHP Script
  2. Set Memory Limit Using Php.ini File
PHP ini_set Memory Limit

Although there are many ways to set memory limits in PHP, the usual way is to modify the php.ini file where the php version is installed on the user machine.

However, it depends on what users want to do with memory modification because the default memory limit allocated for variable consumption is sufficient to execute most tasks.

Set Memory Limit Using PHP Script

We will first echo ini_get("memory_limit") to check what is the current memory limit. Then, we will use ini_set("memory_limit",""); to modify the memory limit.

Note
This method will only apply to the currently used files. And it is also the best practice to use this kind of script rather than affecting the memory limit in the php.ini or .htaccess files, which might cause other problems concerning memory later.

The practical solution to this issue is to create a file, add the following script, and use it to modify the default memory.

<?php
//check memory limit using ini_get
          echo "Current Memory:";
// this will print your current memory limit
    echo ini_get("memory_limit")."<br>";
 //set memory using ini_set
 //format is very important
             ini_set("memory_limit","256M");
 //print new memory limit
         echo "Updates Memory:";
         echo ini_get("memory_limit");
?>

We initially checked the memory limit using ini_get("memory_limit"); function. Then, we used ini_set("memory_limit", "");.

The later function takes two parameters, as shown formerly. You can set your desired memory limit in the function.

Output:

Current Memory:128M
Updates Memory:256M

Set Memory Limit Using Php.ini File

First, you need to determine where your current php is installed root to the PHP directory on your machine.

Once you can locate the php.ini file, search memory_limit. It will show you the max memory a PHP script can consume, which by default must be 128MB.

After that, you have to update it to the desired memory limit. For example: memory_limit = 256M.

On the contrary, you can change it using .htacsess file in the root directory or using C-Panel if you are using C-Panel.

Change memory limit in the php.ini file:

Set Memory using Php.ini File

We have accessed our php.ini file and modified the memory limit in the example above.

It is worth noting that this method applies to all of your php scripts.

You might not want to change it for all the files. Therefore, we recommend using the former method.

So that, all you need to do is define the ini_set("memory_limit", ""); function in your scripts.

References:

  1. PHP ini_set memory limit
  2. PHP Memory limit documentation
Sarwan Soomro avatar Sarwan Soomro avatar

Sarwan Soomro is a freelance software engineer and an expert technical writer who loves writing and coding. He has 5 years of web development and 3 years of professional writing experience, and an MSs in computer science. In addition, he has numerous professional qualifications in the cloud, database, desktop, and online technologies. And has developed multi-technology programming guides for beginners and published many tech articles.

LinkedIn