Run it as a service and it will auto restart.
NOTE: This is not addressing the real problem.
You are probably creating backpressure which is exhausting memory. Unless you share more details (like flows and details of what is happening when) it will be hard to advise the correct approach