Welcome Guest, Not a member yet? Register   Sign In
standalone application with database
#1

[eluser]apolinux[/eluser]
Hi, I'm new in the forum, and sorry for my bad english. I'm developing a standalone app with ci, I mean it's a daemon app,which is always running. I had problems with this app because was crashing, with this fatal error:

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /var/www/html/quattromobile.net/ci/system/libraries/Model.php on line 60.

Basically, the program have a infinite loop, and in the start it connects to a DB, makes a single sql request (update) and after that disconnects.

after a long research process, I could find the solution to this problem. I had to put a breakpoints with the memory_get_usage function and I realize the memory script grows up after each loop. Therefore, soon or late it was to eat all the memory available.

The reason is because the object $this->db request more memory after each loop. My solution was creating the object at the beggining of the loop, then process stuffs, then close the database, and after that deleting the object $this->db, something like this:

Code:
while ( true and ! $this->manejador_senales_sistema->detectoAbortarExterno() )
        {
            $this->db = $this->load->database($this->config_bd , TRUE);
            $this->procesarMos();
            $this->db->close();
            unset($this->db);
            usleep($this->tiempo_espera_bucle);
        }

I hope no more fatal errors appearing with this solution.
any comments are appreciated.
#2

[eluser]Ñuño Martínez[/eluser]
Don't use PHP to create a daemon program. It's a very bad practice, specially if the program has an infinite loop and is always running. PHP is a language to create documents not to do process.

There are a lot of other languages better for that stuff: C, Pascal, Java, Python, REXX...
#3

[eluser]renownedmedia[/eluser]
What you could do is execute a cron job every minute, and setup a txt file which you could use as a lock. Write a 1 if your script is running and a 0 if it is not. When you execute the script every minute, write a 2 to the file, and the last execution could see that it is his turn to end, and the next execution could sleep(3) after starting, then start going. That way, you kill a script execution every minute once a new one has started, which should keep your memory down.

Also, look into sleep() and set_time_limit().

Not sure of any reason to run a program with an infinite loop honestly... Maybe instead of PUSHing data out to people you should have the data PULLed?
#4

[eluser]apolinux[/eluser]
I use the program to read sms messages and process them. The last solution was made with cron, but the messages are coming at any time. The purpose of an infinite loop is efficiency.
#5

[eluser]Ñuño Martínez[/eluser]
[quote author="apolinux" date="1248287327"]I use the program to read sms messages and process them. The last solution was made with cron, but the messages are coming at any time. The purpose of an infinite loop is efficiency.[/quote]
If you need efficiency to process SMS messages then PHP is not the better language. As I've said other languages as C, PASCAL or Java will do the work much more efficiently than PHP.




Theme © iAndrew 2016 - Forum software by © MyBB