Jump to content

PHP CLI Freeing Memory


ezekiel61

Recommended Posts

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.

[code]
#!/usr/local/bin/php -c /usr/local/etc/php-cli.ini
<?PHP
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";
sleep(10);

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

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

unset($test);

echo "\nSleepy Time\n";

// Sleep command so we can watch the memory
sleep(30);
?>

[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.