Core Java

What causes OutOfMemoryError?

OutOfMemoryError might be thrown when one of the following circumstances occurs:

  • JVM run out of native memory
  • Java heap is out of memory
  • PermGen or Metaspace run out of memory
  • JVM spent too much time trying to collect the garbage

The root cause of OutOfMemoryError can be usually deducted from the error message. Let’s look into the details of each of the circumstances.

JVM run out of native memory

It basically means that the amount of memory allocated for the JVM has run out. Maximum size of a process for 32-bit JVM is roughly 3.5 – 4 GB. If it is exceeded, the OutOfMemoryError will be thrown. Even in a 64-bit JVM, when the JVM requests more memory, the operating system might simply not have enough of it. Have a look at the following snippet:

for (int i = 0; true; ++i) { 
  new Thread() { 
    public void run() { 
      try { 
      } catch(InterruptedException e) { } 

   System.out.println("Thread"; + i + "created"); 

On my notebook (64-bit Mac OS X 10.11.6 with Java 1.8.0_112), the JVM crashes after 2023 threads have been created:

Thread 2021 created
Thread 2022 created
Thread 2023 created
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread

Java heap is out of memory

This one is pretty obvious. Too many objects have been allocated so they didn’t fit in the heap space configured for the JVM. Increasing the heap size sounds like a solution, but if it is caused by a memory leak, it will just postpone the OutOfMemoryError. The error message is pretty clear:

Exception in thread “main” java.lang.OutOfMemoryError: Java heap space

PermGen or Metaspace run out of memory

PermGen (Java 7 and earlier) has limited maximum size. It means that if too many classes are loaded, PermGen may fill up and OutOfMemoryError will be thrown. Increasing the maximum PermGen size should help. Java 8 doesn’t have PermGen, but Metaspace instead. By default, it has unlimited maximum size, so as long as you don’t set the limit by MaxMetaspaceSize flag, the error should not be thrown. To diagnose OutOfMemoryError caused by PermGen or Metaspace, the error message should be examined:

Exception in thread “main” java.lang.OutOfMemoryError: PermGen space
Exception in thread “main” java.lang.OutOfMemoryError: Metaspace

JVM spent too much time trying to collect the garbage

This is the trickiest one – OutOfMemoryError is thrown when GC is spending too much time collecting the garbage with too little result and further application execution is pointless. In other words, all of the following conditions must be met:

  • More than 98% of time is spent in GC (98% is the default value, it can be overridden by GCTimeLimit=N)
  • Less than 2% of the heap is reclaimed during the full GC (again, 2% is a default value, it can be overriden by GCHeapFreeLimit=N)
  • Both of the conditions mentioned before are true for five consecutive full GC cycles
  • UseGCOverheadLimit flag is not disabled (true is the default value)

Running a full GC means that JVM is running out of memory anyways. If 98% of time is spent to free only 2% of the heap, then it means that CPU is almost completely busy with GC and practically no application logic can be done. That’s why it makes sense to give up and throw the OutOfMemoryError with the following message:

Exception in thread “main” java.lang.OutOfMemoryError: GC overhead limit exceeded
Reference: What causes OutOfMemoryError? from our JCG partner Grzegorz Mirek at the > performant code_ blog.

Grzegorz Mirek

Grzegorz is a software developer from Cracow, Poland. He started his adventure with Java roughly 6 years ago when he was at university and since that time, he keeps expanding his knowledge in this field. He is especially interested in JVM performance and optimisations and this is what he mostly blogs about.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button