1

I would like to know, is there any issues(bad to server) while using ini_set('memory_limit', '-1') on number of occasions?

The reason why I need to know this is, I'm now working in a project where use of join queries is high. We are only fetch required data's to avoid maximum load but in some situation where data exceeds 20000+ record, execution dies & shows 500 (Internal Server Error).

I know Error 500 may be cause of lot other issues, so I have checked it & found the reason was because of PHP execution memory limit exceeding(256M). When I set ini_set('memory_limit', '-1') on my code, query executed correctly.

The above explanation is just a situation/scenario, no need to consider that & Yes, I agree that using ini_set('memory_limit', '-1') is a bad practice. So, my question is clearly to know is there any issue on using ini_set('memory_limit', '-1') multiple times on different sections of a project (like sever issues or so). If yes, what are they?

Sinto
  • 3,915
  • 11
  • 36
  • 70
  • 4
    The reason for such limit is to protect your system from scripts out of control. So raising that limit is a thread to your system. Question is why your scripts need such a huge amount of memory, _that_ is what you should fix instead of raising that limit. Typically the issue is that query results are copied in memory, sometimes multiple times, instead of processing them in a serial manner. This results in the script using a memory footprint raising in size depending on the query result instead of a constant memory usage. A sign of a questionable implementation. – arkascha Apr 18 '17 at 05:49
  • 3
    Setting the memory limit to "unlimited" is indeed a _very_ bad idea. A few requests and your server will go down. Why would you ever need to fetch 20000+ records in one go? I would rather rethink the architecture. – M. Eriksson Apr 18 '17 at 05:49
  • Im a developer I know its bad to take lot data, client requirements need to be satisfied. So @arkascha, I do not requires unlimited memory, I will check how much memory is required & set it as you suggested. Good suggestion that. – Sinto Apr 18 '17 at 05:54
  • 2
    "Why would you ever need to fetch 20000+ records in one go?" Looks like @Sinto needs to look up a concept called Pagination... – justderb Apr 18 '17 at 05:54
  • As long as you're in testing process, you can go for an infinite limit(take care of your computer, tho).Otherwise, it's a bad idea to allocate whole memory to PHP. – LalaByte Apr 18 '17 at 05:55
  • @justderb, :-) pagination's are all here man. requires some count & count related different calculations. – Sinto Apr 18 '17 at 05:55
  • No, that is _not_ what I suggested. I did _not_ at all say: leave everything as it is, just find out what memory size you _really_ need. I said: fix your code to process the data in a clean serial manner without doubling the data again and again in memory. – arkascha Apr 18 '17 at 05:56
  • You can still do count operations by paging your queries... Just take each page and distill it into the data you need and throw out the rest to keep memory low. – justderb Apr 18 '17 at 05:57
  • @arkascha, yes, i will do that. I not going to leaving the code with unlimited memory, will check & set a limit. – Sinto Apr 18 '17 at 05:58
  • Is that a joke? That last comment reads as if you refuse to read or understand what I wrote by purpose. – arkascha Apr 18 '17 at 05:59
  • @justderb, good. – Sinto Apr 18 '17 at 05:59
  • 1
    I suggest you post the relevant parts of your code where the query result is processed. – arkascha Apr 18 '17 at 06:00
  • @arkascha, No jokes on work. some time I understood wrong, sorry man. – Sinto Apr 18 '17 at 06:00
  • _"client requirements need to be satisfied"_ - Sure, but it's your job to do it well and fetching 20000+ records in one go isn't it. You can fetch data in batches instead of it all in one go. You haven't even told us _why_ you need all those records. Print to screen? XML? Excel? Json? API? – M. Eriksson Apr 18 '17 at 06:01
  • @arkascha & magnus, got that. – Sinto Apr 18 '17 at 06:02

1 Answers1

1

ini_set will set the configuration option during the script's execution, and will be restored at the script's ending.

so ini_set will only works on the current php process. The next time a request coming, a new php process will read php.ini first to get the memory_limit and then you can use ini_set to set the value to -1.

you can use ini_get to check memory_limit to avoid set memory_limit multiple times, and check the return value of ini_set to ensure success.

using ini_set('memory_limit', '-1') is a bad idea. Try to use pagination to avoid fetch all data at a time.

tcPeng
  • 296
  • 1
  • 11