NYCPHP Meetup

NYPHP.org

[nycphp-talk] Memory Limit

Kayra Otaner kayraotaner at yahoo.com
Fri Jan 3 14:53:30 EST 2003


Hi,

I've been trying to find the correct code to show memory usage and 'unset' feature. I was using
Php cli version 4.1 and there was some sort of gc problem I guess. Here is a sample code, it
basically reads a file and once in a  5000 lines it prints usage status. You'll observe that
memory amount dedicated to script is increasing in time. If you comment 'unset()' functions in the
code below, memory consumption speed will going to increase. I'm using 300 Mb of access.log file
so you can observe memory increase after a while. I've checked same code on php cli 4.3 and there
seems to be a fixed but memory dedicated to the script is still increasing after some time. 

$fp = fopen("access.log", "r");
while ($row = fgets($fp, 10000)) :
	$text = $text.$row;
	$i++;
	if ($i > 5000) {
		print_r(getrusage());
		unset($text);
		$text = $row;
		unset($i);
		$i = 0;
		}
	unset($row);
endwhile;


Kayra Otaner


--- Sterling Hughes <sterling at bumblebury.com> wrote:
> > <paralist>This message contained 1 file(s) and is available at
> http://nyphp.org/list/paralist_archive.php?L_mid=2111</paralist>
> > 
> > --- Sterling Hughes <sterling at bumblebury.com> wrote:
> > > > Hi,
> > > > 
> > > > There are two simple ways to have more free memory for your scripts :
> > > > 
> > > > 1- You can increase memory limit from 8 Mb to any amount in php.ini file.
> > > > 2- You can use 'unset()' function to unset unused variables right after
> > > you use them.
> > > >
> > > 
> > > err, that should only be php 3 specific, php4's reference counting
> > > mechanism releases all
> > > variables the second they are no longer referenced in the script.
> > 
> > But how would they become unreferenced without unset()ing them?
> >
> 
> reference counting, that's how it works. :-)  once you no longer reference
> a variable in your script it is destroyed.  Same applies for resources, like
> filehandles. One they are no longer used, the files are closed.
> 
> http://www.zend.com/zend/art/ref-count.php
> 
> Now of course if you keep a lot of variables in one scope, then you should 
> explicitly free them, but chances are, if you're code is good, it should be modularized.
> If you really do need to deref a variable, the best way is by:
> 
> $var = NULL;
> 
> -Sterling
> 
> > 
> > > > --- "Nunez, Eddy" <enunez at tiaa-cref.org> wrote:
> > > > > 
> > > > > This is in reference to the standard memory limit for server PHP
> > 
> > > scripts,
> > > > > which is 8MB.
> > > > > 
> > > > > My question is...
> > > > > is there any function to query available free space in the memory pool?
> > 
> > Maybe getrusage() would be handy, and I've attached a cheesy example I've
> > used to quickly profile scripts, and the posix_* functions look good, too. 
> > Plus, you could always run the UNIX commands and parse the output for mem
> > usage, what's free, etc on a system wide basis.
> > 
> > 
> > =====
> > Hans Zaunere
> > President, New York PHP
> > http://nyphp.org
> > hans at nyphp.org
> > 
> > 
> > 
> > 
> > 
> 
> 
> --- Unsubscribe at http://nyphp.org/list/ ---
> 
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com



More information about the talk mailing list