I use for my project in Symfony 5.3, the Messenger Component with a RabittMQ server. I want to manage my memory of my MessageHandler because my code is taking too much memory (Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 33554440 bytes
).
For each message consumed, I feel like the MessageHandler
retains the memory of the previous MessageHandler
. This is my class where I run a command:
class MessageHandler implements MessageHandlerInterface
{
private KernelInterface $kernel;
public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
}
/**
* @param RequestMessage $requestMessage
* @throws \Exception
*/
public function __invoke(RequestMessage $requestMessage)
{
$application = new Application($this->kernel);
$application->setAutoExit(false);
$input = new ArrayInput([
'command' => 'app:my-command',
'userId' => $requestMessage->getUserId(),
'--no-debug' => ''
]);
$output = new BufferedOutput();
$application->run($input, $output);
}
}
And I consume my messages with this command:
$ php bin/console messenger:consume -vv
I am looking for a solution to consume each of my messages with an independent memory. I don't know where the problem is, if someone can help me.
I can think of a memory leak but I don't understand why the memory of a message is not cleaned.
First of all, I am aware that I have a memory issue but I have to find a solution.
To run my command, I use Symfony's Process component to run my command in a different process to not overload RAM and run it independently. This is my resolve code:
use Symfony\Component\Process\Process;
$completedCommand = 'php bin/console app:my-command ' . $user->getId() . ' --no-debug';
$process = Process::fromShellCommandline($completedCommand);
$process->run();
if (!$process->isSuccessful()) {
// Example: Throw an exception...
}