Excessive consumption of memory due to precompiling assets

It’s always advisable to have swap. It’s a very good idea to enable the kernel to overcommit. It can substantially reduce your peak memory needs.

I’d sort out those two things and then try again. For overcommit, see

In terms of diagnosis, it might be helpful to check dmesg for OOM events, which you could do afterwards, and also to run vmstat at the time of the stall.

vmstat 5 5

Here’s my general diagnostic advice: