This simply means that for some reason garbage collector is consuming an excessive amount of time. For example, 98% of CPU time is spent on GC and less than 2% of the heap is recovered. This technically means your program stops making any progress and just executes the garbage collector. Thus in order to let you diagnose this error and prevent the applications from running for an extended period, JVM throws this error.
You can prevent this error, by following any of the below:
Increasing the heap size, for e.g: -Xmx1g
Enabling the concurrent low pause collector -XX:+UseConcMarkSweepGC
Reusing the existing objects when possible to save some memory
Hope this helps!!