Problem scenario
You are getting an error such as "java.lang.OutOfMemoryError java heap space"
You have plenty of swap space available, but this error persists. The problem is as if nothing is using swap space you know exists. Why won't your Java program or Maven build utilize the swap space that is available to it?
Possible solution #1 (for Maven builds)
Run a command like one of these (but uncomment the second one if you want to commit roughly 5 GB of memory to the maven build):
export MAVEN_OPTS=-Xmx512m
# export MAVEN_OPTS=-Xmx5120m
Now retry your mvn command.
Possible solution #2 (for Java programs)
Make sure you are using an option like -Xmx512m with you Java program.
Possible solution #3
Run these four commands for clues:
jps -lv
free -mh
df -h
top
Possible solution #4
Try rebooting the server.
Possible solution #5
Read this external article: https://www.cyberciti.biz/faq/linux-which-process-is-using-swap/
Possible solution #6
Changing the heap size can cause the "out of memory" error. You may need to change the heap size to something different.