I have a snippet that resembles the following:
while (true) {
$myObject = new Class();
$myOtherObject = $myObject->getSomeOtherObj();
...
$myArray = [1, 2, 3];
...
sleep(1); //Sleep at the end, to save CPU.
}
This snippet should run as a daemon service, but I'm having a lot of trouble making this work.
The issue: each iteration increases the process memory usage. As if at each new iteration a new $myObject
is being instantiated, but the previous one remains allocated in memory, and such.
I have tried:
- to
unset
all variables at the end of the loop (right before thesleep()
). - Setting all variables to
null
. - encapsulating them in a separate function (
while (true) { doThis(); }
) - manually calling
gc_collect_cycles()
None of those worked to decrease memory usage.
I have no idea how to force all memory to be released.