Jump to content


PHP CLI Freeing Memory

  • Please log in to reply
No replies to this topic

#1 ezekiel61

  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 25 October 2006 - 05:46 AM

After much googling I'm stumped.

I have a PHP CLI application that runs as a constant background process on our server. As the process does its job it slowly builds up its memory usage. I've tried to combat this by using the unset() function on my objects that are no longer needed, but that doesn't help and I can't recover that memory. Has anyone else had this problem.

Here is a sample CLI script that shows the problem. I build up a good bit of memory while I'm processing and I'd like to free it when the job is done.

#!/usr/local/bin/php -c /usr/local/etc/php-cli.ini
class TestObj{
	private $testArr;

	public function __construct(){}
	public function __destruct(){}

	public function testme(){
		$this->testArr = array();

		for($i = 0; $i < 100000; $i++){
			$something = "";

			for($j = 0; $j < 10; $j++){
				$something .= chr(rand(35,126) );
			$this->testArr[$i] = $something;
$loop = true;

echo "Initial Memory Usage: " , memory_get_usage() , "\n\n";

echo "Creating New Object\n\n";
$test = new TestObj();
echo "Running Test Function\n\n";

echo "Ready to destroy object in...";
for($x=5 ; $x > 0; $x--){
	echo $x, "...";


echo "\nSleepy Time\n";

// Sleep command so we can watch the memory

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users