Web Hosting Talk







View Full Version : PHP memory usage


pmak0
11-06-2002, 10:20 PM
How do I configure php.ini so that PHP scripts that go into an infinite loop won't consume up all the system resources? I want to make it so that each script isn't allowed more than 10 MB of memory or something like that.

Sonic Blue
11-06-2002, 11:03 PM
void set_time_limit (int seconds)

there should be a setting in your PHP Configuration but if its not your server then hopefully that willwork.

The good stuff is more towards the end ;)

One thing to note is that both the ABORTED and the TIMEOUT states can be active at the same time. This is possible if you tell PHP to ignore user aborts. PHP will still note the fact that a user may have broken the connection, but the script will keep running. If it then hits the time limit it will be aborted and your shutdown function, if any, will be called. At this point you will find that connection_timeout() and connection_aborted() return true. You can also check both states in a single call by using the connection_status(). This function returns a bitfield of the active states. So, if both states are active it would return 3, for example.


void set_time_limit (int seconds)

Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the configuration file. If seconds is set to zero, no time limit is imposed.

When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.

Note that set_time_limit() has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the configuration file.

int ignore_user_abort ([int setting])

This function sets whether a client disconnect should cause a script to be aborted. It will return the previous setting and can be called without an argument to not change the current setting and only return the current setting.

pmak0
11-07-2002, 01:50 AM
So I could type "max_execution_time 30" in php.ini to prevent a script from executing for more than 30 seconds, I suppose.

What about memory consumption limit?

Sonic Blue
11-07-2002, 01:45 PM
encase the numbers in brackets (30); because it is a function

I wouldnt be able to take a quess at how you lmit the Memory loss. It really cant be done. thats up to the configuration of the Server. Technically the Memory should be recovered when the script is finished executing be if completting or tetermination

rusko
11-07-2002, 04:13 PM
implement the remarks ifrom the previous posts to have the script abort if it goes into an infinite loop - while it may not consume that much memory, it will certainly tie up the cpu.

for the memory limit, you need to compile php with --enable-memory-limit (argument to configure) and set the memory_limit in php.ini

simple as that =]

good luck,
paul

rusko
11-07-2002, 04:14 PM
oh yes, almost forgot, rtfm =]