5

Getting java.lang.OutOfMemoryError : java Heap space on Jboss 7 The entry in jboss configuration is

set "JAVA_OPTS=-Xms1G -Xmx2G -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=2096M"

The Error was

 java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOf(Arrays.java:3332) [rt.jar:1.8.0_231]
        at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124) [rt.jar:1.8.0_231]
        at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448) [rt.jar:1.8.0_231]
        at java.lang.StringBuffer.append(StringBuffer.java:270) [rt.jar:1.8.0_231]
        at java.io.StringWriter.write(StringWriter.java:112) [rt.jar:1.8.0_231]
        at java.io.PrintWriter.write(PrintWriter.java:456) [rt.jar:1.8.0_231]
        at java.io.PrintWriter.write(PrintWriter.java:473) [rt.jar:1.8.0_231]
        at java.io.PrintWriter.print(PrintWriter.java:603) [rt.jar:1.8.0_231]
        at java.io.PrintWriter.println(PrintWriter.java:756) [rt.jar:1.8.0_231]
        at java.lang.Throwable$WrappedPrintWriter.println(Throwable.java:765) [rt.jar:1.8.0_231]
        at java.lang.Throwable.printEnclosedStackTrace(Throwable.java:698) [rt.jar:1.8.0_231]
        at java.lang.Throwable.printEnclosedStackTrace(Throwable.java:710) [rt.jar:1.8.0_231]
Rajeev Ranjan
  • 444
  • 1
  • 11
  • 17

7 Answers7

3

You are encountering OutOfMemoryError: Java heap space,in this case you dont have to increase MetaSpace. I will suggest you to increase heap allocation (Xms3G -Xmx3G). Make sure you have same values for Xms and Xmx. If you still encounter same issue with this then add -XX:+HeapDumpOnOutOfMemoryError option. This option will generate heap dump when OOM error occurs. You can analyze this heap dump through tools like eclipse mat to check which objects consume more memory and if there is any memory leak.

Abhijit Humbe
  • 1,303
  • 1
  • 9
  • 11
2

java.lang.OutOfMemoryError: OutOfMemoryError usually means that you’re doing something wrong, either configuration issue(where the specified heap size is insufficient for the application) or holding onto objects too long(this prevents the objects from being garbage collected), or trying to process too much data at a time.

Possible Solutions:

1) Try setting "JAVA_OPTS=-Xms1G -Xmx1G -XX:MaxPermSize=256M " to maximum value and restarting your server.

2) Check your code for any memory leak. Use some heap dump reader to check the same. (There are multiple plugins available for IDE's like Eclipse, IntelliJ, etc)

3) Check your code(90% of the times issues are in the code): Check if you are loading excess data from a database or some other source into your heap memory and if it's really required. If you are calling multiple web-services and multiple DB Read-only operations cross-check the db query(If Joins are perfectly used with right where clauses) and amount of data returned from db query and web service.

4) If the issue is due to some recent code change, then try to analyze the same.

5) Also, check if cache and session elements are cleared once used.

6) To be sure if the issue is not due to Jboss, you can run the same code on some other server for testing purpose(Tomcat, Websphere, etc)

7) Check Java documentation for more understanding on Out of memory error: Documentation Link

Shailesh Yadav
  • 931
  • 14
  • 30
2

in case you use IntelliJ use the following image inside VM option add what you need

enter image description here

Mina Fawzy
  • 18,268
  • 13
  • 118
  • 129
1

Make sure you have provided enough space in your standalone.conf file inside bin directory

 JAVA_OPTS="-Xms512m -Xmx1024m -XX:MetaspaceSize=512M -XX:MaxMetaspaceSize=1024m -Djava.net.preferIPv4Stack=true"

You should have to increase MaxMetaSpaceSize upto 1024m and MetaspaceSize to 256m hope it will works.

manikant gautam
  • 3,109
  • 1
  • 15
  • 24
1

Depending on the application deployed on a JBoss, even 2 GB of a heap could be not enough. Potential problems:

  1. Xmx configuration is not applied (configuration is made in a wrong file)
  2. The application just requires more heap
  3. There is a memory leak in the application

If you run JBoss on Windows, set in standalone.conf.bat file in the JAVA_OPTS variable the following values -Xmx2G -XX:MaxMetaspaceSize=1G.

standalone.conf file is ignored on Windows and applied on *nix systems only.

Verify that these values are applied by connecting to the JVM using JConsole (that is a part of JDK) or JVisualVM.

Using these tools you can monitor heap usage and see if more heap is required.

If the heap size is big enough (e.g. 4+ GB) and heap still constantly grows while garbage collection (GC) doesn't free space, probably there is a memory leak.

For analysis add to the JAVA_OPTS the following flags: -Xloggc:gc.log -XX:+PrintGCTimeStamps -XX:+HeapDumpOnOutOfMemoryError.

With these flags JVM will log GC activity into the gc.log file where you can explicitly see how much space is freed after the GC.

If subsequent GC executions according to the log doesn't free any space, probably there is a memory leak and you should analyze a heap dump created manually using JVisualVM or created by JVM itself on OutOfMemoryError. Heap dumps can be analyzed using JVisualVM or Eclipse Memory Analyzer (MAT).

Evgeniy Khyst
  • 5,283
  • 1
  • 19
  • 45
1

Try also to monitor your process , try to run jvisualvm ( is included into the jdk ) and a UI will open , from there you will be able to get a lot of info.

Hope this help you.

pioardi
  • 163
  • 2
  • 6
1

Usually, this error is thrown when there is insufficient space to allocate an object in the Java heap. In this case, The garbage collector cannot make space available to accommodate a new object, and the heap cannot be expanded further.

Please do the below possible steps for fixing java.lang.OutOfMemoryError.

Step 1: Check JBoss configuration settings. The setting depends on your system configuration, java version and JBoss version. Check configuration here.

Step 2: Check Heap dump settings here.

Step 3: Java code/ Library problem.

Analyze your memory here And Jboss memory profiler.

Other causes for this OutOfMemoryError message are more complex and are caused by a programming error.

Naresh Kumar
  • 721
  • 7
  • 24