I see your application tends to restart multiple times due to the error it faces, java.lang.OutOfMemoryError: Java heap space. This usually happens when the allocated JVM heap memory is not sufficient to handle any executions based on the application’s memory requirements.
In order to address this, the basic thing you will have to do is to increase the heap size if your infrastructure allows it. For example, increasing both -Xms and -Xmx to 6GB or higher might help stabilize the application. Along with this, I will suggest that it's a good idea to enable heap dump generation on OutOfMemoryError. By doing so, you will be generated with a memory dump file whenever an issue occurs. Now you can analyze using tools like Eclipse MAT or VisualVM to identify what’s consuming so much memory.
Apart from this, you should try to enable GC (Garbage Collection) logs. The logs generated will help you to monitor how memory is being allocated and released. This will give you an idea of how GC is happening, its frequency and also will point out to us if any unnecessary objects are present.
In general check for memory leaks, large objects being retained in memory, or inefficient use of collections and caches. Once you start to optimize these areas you can find a significant reduction of memory pressure.
Suppose, if your application is currently using the default or Parallel GC, consider switching to G1GC. This is actually better suited for handling larger heaps and providing more predictable pause times.
At last, I would say the setting -XX:ParallelGCThreads=16 might be unnecessarily high for a 5GB heap unless you're running on a high-core machine. Try to tune this value to match your CPU resource. This will also have an improved performance.
After making these adjustments, monitor your application’s memory behavior and GC logs again to verify the improvements.