How to set memory free in for loop with php?

I want to generate an infinity loop to do some processing. Inside the loop i destroy every single variable by using 'unset' . I thought it should free some memory for the garbage collection but in fact the process keep holding the object and allocated memory keep increasing. It seems like the destruct() function never work ?

NuSoap

Thanks for your reply. I will try the crontab and monitor the process to see how it goes.

By the way, do you know nusoap? Recently i tried to build a client by using nusoap and communicate with the server with Microsoft-IIS/5.0 (ASP.NET).

I always get the Internal Serever Error which is show as below:

ResponseHTTP/1.1 500 Internal Server Error
Server: Microsoft-IIS/5.0
Date: Thu, 04 Oct 2007 09:09:58 GMT
X-Powered-By: ASP.NET
Content-Type: text/xml; charset="UTF-8"
Content-Length: 767
Expires: -1;

And the fault report from soap:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

SOAP-ENV:Server
An unanticipated error occurred during the processing of this request.

-2147418113

<HRESULT>8000ffff</HRESULT>

I really run out of idea, because sometimes i can get Response success 200 but sometime will get 500 (internal server error) response by using the same param and the same script.

Example of client :
http://isosalis.com/gateway/test2.php

Example of Server:
http://202.75.163.45:2503/MCPECPAMT.WSDL

Really appreciated if you can help on this.

Best Regards,
Victor

Comments

I only can suggest to debug the WebService and see what happens or google for this issue. I have not work with SOAP a lot.

BTW, it is better to create different forums threads for each issue.

Sincerely,
Alex

It highly depends on the code of your loop

Ideally, PHP garbage collector destroys unused objects immediately after they become dereferenced. The exact behavior highly depends on code and/or PHP version. It would be great if you could create a small example that demonstrates the problem.

Sincerely,
Alex

Example of the looping

Here is the example,

<?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;
}
}
}

//Perform infinity loop
for($i=0; $i < 2){
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";

//Goto Sleep so we can monitor the memory

sleep(30);
}
?>

I run the script at background process, the process "Initial Memory Usage" around 515240 ... and when i start posting data to the loop ($something) X 1000 times the memory usage will increase. But it will immediate decrease after sleep (30) . My question is , can i free some memory again from the initial process instead of it keep holding the buffer 515240 for the loop ?

Any comment will be very much appreciated.

thanks.

regards,
victor

Use separate PHP process

It looks like it is not related to the garbage collector but for how PHP works with memory at all. It seems that it just "reserves" some cache and does not free it at any circumstances.

I can recommend you to use a separate PHP process or use crontab instead of "infinite loop".

Sincerely,
Alex

Crontab

Hi Alex,

I would like to use crontab. But my concern is, let say if i set a cronjob to run the script every second which involved data validation, generate unique ID, and some transaction to delete unused data. Do you think that the process will crash somehow if the "second worker" start functioning before the "first worker" finish their job?

Actually i try to create a real time environment which the connection will never stop. The communication between server and server. So i need some backend engine to process those data which will never end. And the response time between server must less than 9 seconds, otherwise the memory will be exhausted .

Any solution that i can referring to?

thanks for your reply. appreciated...

Best Regards,
Victor

Reply

>Do you think that the process will crash somehow if the "second worker" start functioning before the "first worker" finish their job?

You need to handle this situation in the script. For example, the script can wait until the first worker finish its job or just exit. The script can create a folder, called .lock, at the start and delete when it completes its job.

Generally, I think that PHP is good enough to be used for what you described.

Sincerely,
Alex