• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Types of OutOfMemoryError

How to generate and resolve OutOfMemoryError?

June 2, 2017 By j2eereference Leave a Comment

OutOfMemoryError in java can occur because of many reasons like Java heap space, GC Overhead limit exceeded, requested array size exceeds VM limit and many more. It is an indication of a memory leak .

Listed below are types of java.lang.OutOfMemoryError Error messages:

  • java.lang.OutOfMemoryError:Java heap space -> This exception is thrown when JVM heap size if full and the garbage collector is unable to reclaim objects
  • java.lang.OutOfMemoryError:Metaspace ->this exception is thrown when Metaspace area is exhausted.
  • java.lang.OutOfMemoryError:Unable to create new native thread ->this exception is thrown when java process size has reached its maximum limit(too many threads are spawned in a process)
  • java.lang.OutOfMemoryError:GC overhead limit exceeded->This exception is thrown when your application spends too much time doing garbage collection.
  • java.lang.OutOfMemoryError:Permgen space -> This exception is thrown when is thrown when PermGen space memory is full.
  • java.lang.OutOfMemoryError:Out of swap space -> This exception is thrown when when the JVM memory is full when there are too many processes running on the machine.
  • java.lang.OutOfMemoryError:Requested array size exceeds VM limit -> This exception is thrown when application is trying to allocate an array whose size exceeds than the Java Virtual Machine.

In this tutorial lets understand about OutOfMemoryError which is thrown when there is limited or insufficient space to allocate an object in the Java heap i.e java.lang.OutOfMemoryError: Java heap space

 Program to generate OutOfMemoryError: Java heap space

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.j2eereference;
 
import java.util.ArrayList;
import java.util.List;
 
public class OutOfMemoryDemo
{
   private static List<String> heapList=new ArrayList<String>();
   public static void main(String[] args)
     {
     while(true)
           {
             heapList.add(new String("Shobhna"));  
           }
      }
}

OutPut:
Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3210)
at java.util.Arrays.copyOf(Arrays.java:3181)
at java.util.ArrayList.grow(ArrayList.java:261)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227)
at java.util.ArrayList.add(ArrayList.java:458)
at com.j2eereference.OutOfMemoryDemo.main(OutOfMemoryDemo.java:11)
How to solve OutOfMemoryError – Java heap space ?
To solve OutOfMemoryError we need to increase the heap size using jvm parameter –Xmx

How to use -Xmx VM option in java
1) If you want to set the maximum heap size of JVM to 1024 bytes then use below command
java -Xmx1024 OutOfMemoryDemo

2) If you want to set the maximum heap size of JVM to 1024 kilobytes then use below command
java -Xmx1024k OutOfMemoryDemo

3) ) If you want to set the maximum heap size of JVM to 1024 megabytes then use below command
java -Xmx1024m OutOfMemoryDemo

4) If you want to set the maximum heap size of JVM to 1024 gigabyte then use below command
java -Xmx1024g OutOfMemoryDemo

 

 

Related Posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof

Filed Under: Core Java Tagged With: -Xmx parameter usage, Heap space error, OutOfMemoryError, to resolve OutOfMemoryError, Types of OutOfMemoryError

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.