Mobile app version of vmapp.org
Login or Join
Si4351233

: Using ini_set() to change memory lmit for individual PHP scripts I have had trouble in the past with memory issues caused by individual scripts on a website using more memory than they should.

@Si4351233

Posted in: #Php

I have had trouble in the past with memory issues caused by individual scripts on a website using more memory than they should. I have tackled this problem by lowering my PHP memory limit from 512MB (the requirement of one script on the site) to 64MB, and using ini_set("memory_limit", "512M") on the individual script that needed it.

The question is whether this is a good method, or if there are better ways of tackling this issue. The other method I have seen is adding a second php.ini file in the directory of the script, but this will not work because almost all of the scripts for the website are in the same folder.

Please note, I am aware that even a 64MB memory limit is high for most sites, but it is required by the platform I am using.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Si4351233

2 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

It actually makes sense to increase the limit directly in php.ini, since the processes that don't consume the currently allocated 64MB you have now, won't go over this limit anyway.

The only drawback in doing this is that if you have more processes consuming large amounts of memory, they will slow down your system and the server can launch fewer processes in the amount of RAM you have allocated on your machine (but only if the script or page that's eating up your memory is frequently accessed in multiple instances).

However, you should try to optimize your code and prevent this from ever happening. The most common mistake here is to get a large amount of results from the database and try to keep them in a large array or object in your script for processing at a later time in the code - and there are some ways of preventing this:


use lazy-loading techniques (load data from cache or database only the moment you need it and get only the data you need) and optimize your database queries (don't do a SELECT * FROM table if you only need the id and name fields)
use database cursors to iterate through your result set (this is of particular importance if you have really huge amounts of data that won't ever fit into the allocated memory)
only use what you need and don't pollute the memory with redundant or unnecessary information
release the memory used by any large arrays or objects that you're only using temporarily inside various methods and loop structures, by calling unset($variable); and then force the garbage collector to run immediately after by calling gc_collect_cycles();

10% popularity Vote Up Vote Down


 

@Rambettina238

Yes, it's better to have a lower memory limit and raise it for one script than to have a higher limit overall. The only better way to tackle the issue would be to reduce the memory requirements of the 512MB script if that's possible.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme