0

There is a 4gb memory usage per day and it does not clean at all

Have the following php script :

while(1){
    sleep(15);
    $data = // get data

    foreach ($data as $d){
        //get the db status
        // make api call in same ip to update server 
        // write log
    }
}

service linux code (via docker):

[program:cron]
command=/bin/bash -c "exec cron -f"
autostart=true
autorestart=true
startretries=3

What can be done to prevent that? it should be a sample service

M Reza
  • 18,350
  • 14
  • 66
  • 71
Tuz
  • 1,810
  • 5
  • 29
  • 58
  • Why are you foreaching within a while loop? – treyBake May 24 '18 at 06:12
  • @ThisGuyHasTwoThumbs because I need the service runs forever – Tuz May 24 '18 at 06:58
  • then set up a cron job.. this is why you have such high memory usage xD – treyBake May 24 '18 at 07:25
  • @ThisGuyHasTwoThumbs as you can see there is a cron job configured – Tuz May 24 '18 at 08:43
  • then why do you need a while loop to make it run all the time? – treyBake May 24 '18 at 08:45
  • So the cron starts new infinity loop every x time? So there could be like 1000 of those loops? O.o – Eakethet May 24 '18 at 08:57
  • Please show your code. Since Stack Overflow hides the Close reason from you: *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/).* – jww May 25 '18 at 00:52

2 Answers2

-1

It looks like you're using supervisord, why not swap the command out instead of running it through cron:

[program:phpscript]
command=/path/to/php /path/to/script.php
autostart=true
autorestart=true
startretries=3
steadweb
  • 15,364
  • 3
  • 33
  • 47
-1

Most likely you've got some data structure like an array which gets more data appended on each run and is never freed. There's also a chance that inside your loop, you're producing cycles in some way which are not collected immediately.

You can run a quick check by adding gc_collect_cycles(); after the foreach loop. If that solves the problem, great. You can read more about reference cycles at https://secure.php.net/manual/en/features.gc.collecting-cycles.php

Otherwise, you should have a look with a profiler to see what exactly is left allocated. See PHP memory profiling and other similar questions.

viraptor
  • 33,322
  • 10
  • 107
  • 191