Page 1 of 1

infinitiv loops and memory

Posted: Mon Jul 16, 2007 3:06 am
by startx
hello.

i want to experiment with a simple socket (similar to http://devzone.zend.com/node/view/id/1086)
listener script which runs in and endless loop.
(php-cli on ubuntu edgy, php version is 5.1.6 )

to my suprise i noticed that even a simple empty loop like

Code: Select all

#! /usr/bin/php
<?php

while(1) {

}

?>
allready takes 17MB of memory. (right after start)

even if i assume that the whole php interpreter stays in memory
during execution that would be 4 or 5MB. the zend example takes exactly the same memory btw.

whats the efficient way of writing such long running script?

i looked for tutorials but couldnt find any good help.

Posted: Mon Jul 16, 2007 3:15 am
by volka
If you want a lower footprint (re-)compile php with as few built-in modules as possible.

Posted: Mon Jul 16, 2007 3:43 am
by startx
sure, not loading any module (or not compiling them in) reduces the footprint.

however i was wondering if there is more to optimise infinitive loops?
(or is there a good example of a mini-php-cli?)

Posted: Mon Jul 16, 2007 3:56 am
by volka
If you call php.exe without any arguments it is waiting for a php script on stdin.
With all extension=xyz lines commented in php.ini this waiting php.exe process takes about 6MB (php 5.2.3 from php.net)
With my standard configuration it takes 12MB. Without having parsed or executed any script.

Posted: Mon Jul 16, 2007 4:12 am
by startx
If you call php.exe without any arguments it is waiting for a php script on stdin.
ok, i see what you mean.
but does php-cli have to be run with this behavior?

is there a way to execute a script without having php.exe waiting for another input, or is this
the normal way the php interpreter runs every script?

Posted: Mon Jul 16, 2007 5:31 am
by volka
That's the normal way. I only called it without arguments so php.exe stays put and I can take a look at the memory usage.

Posted: Mon Jul 16, 2007 5:54 am
by startx
I only called it without arguments so php.exe stays put
right. but as far as i can see there is no option to prevent that anyway ?
correct me if im wrong.

Posted: Mon Jul 16, 2007 5:55 am
by volka
Sorry, I don't understand what you mean.

Posted: Mon Jul 16, 2007 6:16 am
by startx
ok sorry, maybe that was a bit confused from my side:

you said
I only called it without arguments
but having a look at my example (an infinitiv running script) which is executed as a php-cli script
using the #! /usr/bin/php line

now when i execute this the interpreter stays in the memory (using some MB footprint however big the
actual script is). So technically why is this necessary? Is there a way to avoid that? when you said "im alsways using it without arguments" it sounded as if there would be alternatives ways to do so.

maybe im missing some deeper understanding of the zend engine here and im happy for some links.

and generall, when using "#! /usr/bin/php" it seems there is no way to add any option.

hope this helps to understand my question. :)

Posted: Mon Jul 16, 2007 6:37 am
by volka
startx wrote:when you said "im alsways using it without arguments"
Where did I say that?

Again: I only called php.exe without any arguments to take a look at the memory usage.
You can do the same. Call php-cli without arguments call ps (from another terminal) and take a look at the memory usage. You will see how much memory php uses without even having a script parsed, far from executing one.

Posted: Mon Jul 16, 2007 7:36 am
by startx
yeah, i understood that. my question was if there ARE any command line options which do PREVENT
ph-cli stay in memory.

anyway, i just tried this example of a "daemon" like loop here

Code: Select all

<?
   set_time_limit(0);   // Remove time limit 
 
   if (pcntl_fork()) {  // Fork process
     print "Daemon running.";
   } else {
     posix_setsid();    // Make child process session leader
 
     while (true) {
                        // Daemon script goes here
     }
   }
?>

now interestingly the footprint is less than 50% of the zend example script and
the very simple loop i posted above. however it uses 99% of CPU constantly.

Posted: Mon Jul 16, 2007 7:42 am
by volka
php-cli is executing your script. If php-cli is removed/terminated so is your script.
Therefore the answer to
startx wrote:my question was if there ARE any command line options which do PREVENT ph-cli stay in memory.
is probably "no" although of coourse php-cli can be terminated.

Your new script creates a fork of the current process (a new process that is cloned from the current one). The purpose usually is to let the current script continue its work (or terminate) and let the new process work in the background. If you fork a script that is executed by php-cli your create a new php-cli process as a clone.

Posted: Mon Jul 16, 2007 8:13 am
by startx
If you fork a script that is executed by php-cli your create a new php-cli process as a clone.
i expected that, too. but to my suprise the memory footprint for it is much smaller in comparison
juts running an unforked instance of php-cli.

Posted: Mon Jul 16, 2007 9:25 am
by volka
You probably mean the resident set, i.e the part of the address space of the process that is currently in "real memory".
How did you measure the memory usage?