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.
Deploying to Production
On production, there are a few important things to think about:
Don't Let Workers Run Forever
Some services (like Doctrine's EntityManager) will consume more memory over time. So, instead of allowing your worker to run forever, use a flag like messenger:consume --limit=10 to tell your worker to only handle 10 messages before exiting (then Supervisor will create a new process). There are also other options like --memory-limit=128M and --time-limit=3600.
From: Symfony Messenger Docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With