504 Gateway Time-Out / Out of memory

Hello community.

I’m running my campaigns in different ways.

I have a VPS with Plesk and 4GB RAM with 2 vCore CPUs. When I run the cronjob to process the queue, I get an out of memory error and the server goes down, I need to restart the server to get it back online.

If I use the Process Queue button in the UI I get a 504 gateway timeout and sometimes the server gets slow but never goes down but I get this error and I need to process the queue manually until the queue becomes empty and I have to repeat the same thing.

The question is: what should I do to prevent it from crashing using the cronjob or getting the 504 error?

thanks in advance.