How to make Go routines release their used memory

I only have 8GB memory. The process crashes with status 137 when the values echo 1 100000000000000 | TestPrimes is given. Apparently, this is the OS that kills the process because it uses too much memory. note that there are 14 zeros.

I then tested by reducing the number of OS threads that can run concurrently. Go routines are green threads run by OS threads. GOMAXPROCS=2;(echo 1 100000000000000 | TestPrimes). No crashes.

Without specifying GOMAXPROCS I see 5 TestPrimes threads in htop. I would expect it to be 4 because there are 4 cores. The program outputs “threads = 4”.

By specifying GOMAXPROCS=2, I see only three threads and the program still outputs “threads = 4”. In this configuration the system doesn’t run out of memory.

I suspect that with 5 OS TestPrimes threads the garbage collector can’t run enough and this could explain why it runs out of memory. I will have to ask the experts if that is correct.