DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers
  • All You Need To Know About Garbage Collection in Java
  • Difference Between InitialRAMPercentage, MinRAMPercentage, MaxRAMPercentage
  • JVM Tuning Using jcmd

Trending

  • APIs for Logistics Orchestration: Designing for Compliance, Exceptions, and Edge Cases
  • Why Rate Limiting Matters in Istio and How to Implement It
  • How to Format Articles for DZone
  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  1. DZone
  2. Coding
  3. Java
  4. What Causes OutOfMemoryError?

What Causes OutOfMemoryError?

If you're ever gotten this frustrating message, fear not, it's root causes can usually be gleaned from the type of error message that is thrown. We'll show you how.

By 
Grzegorz Mirek user avatar
Grzegorz Mirek
·
Sep. 01, 17 · Analysis
Likes (18)
Comment
Save
Tweet
Share
38.2K Views

Join the DZone community and get the full member experience.

Join For Free

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

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

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

JVM Has Run Out of Native Memory

It basically means that the amount of memory allocated for the JVM has run out. The 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 {
            Thread.sleep(1000000);
         } catch(InterruptedException e) { }
      }    
   }.start();

   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 Has 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 an 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 using the MaxMetaspaceSize flag, the error should not be thrown. To diagnose an 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 overridden 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 anyway. If 98% of the time is spent to free up only 2% of the heap, then that means that the CPU is almost completely busy with GC and practically no application logic can be performed. 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

garbage collection Java (programming language) Java virtual machine Memory (storage engine)

Published at DZone with permission of Grzegorz Mirek, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers
  • All You Need To Know About Garbage Collection in Java
  • Difference Between InitialRAMPercentage, MinRAMPercentage, MaxRAMPercentage
  • JVM Tuning Using jcmd

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!