• Skip to primary navigation
  • Skip to 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

OutOfMemoryError

Difference between Stack and Heap memory

June 15, 2017 By j2eereference Leave a Comment

Lets see some of the main differences between Stack and Heap memory.

1) Stack memory is used to store local variables and references to other objects in the method whereas heap memory is used to store objects.
2) Size of heap memory is much bigger than the stack memory.
3) Each thread has their own stack which stores local variables and these variables are not accessible globally whereas objects stored in heap are globally accessible.
4) Heap memory size is bigger where we can create and store objects in any order whereas stack memory follows LIFO(Last In First Out) order.
5) We can change the size of heap memory using jvm parameters : -Xms for initial heap memory and –Xmx for maximum heap memory. For stack , use –Xss to specify stack size for each thread.
6) If stack memory is filled up then you will get java.lang.StackOverFlowError . If there is no space left to allocate new Object in Heap memory then you will get java.lang.OutOfMemoryError: Java Heap Space.

Program to generate StackOverFlowError  exception :

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.j2eereference;
public class StackOverflowErrorDemo{
  public static void recursiveComputation(int count)
  {
    if(count == 0)
      return;
    else
      recursiveComputation(++count);
   }
  public static void main(String[] args)
   {
      StackOverflowErrorDemo.recursiveComputation(10);
   }
}

OutPut:
Exception in thread “main” java.lang.StackOverflowError
at com.j2eereference.StackOverflowErrorDemo.recursiveComputation(StackOverflowErrorDemo.java:13)
at com.j2eereference.StackOverflowErrorDemo.recursiveComputation(StackOverflowErrorDemo.java:13)

Program to generate OutOfMemoryError: Java heap space

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.j2eereference;
 
import java.util.ArrayList;
import java.util.List;
public class OutOfMemoryDemo
{
    private static List<String> heapList=new ArrayList<>();
    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

 

7) Stack memory allocation is simple as compared to heap memory allocation as Stack uses LIFO order,so stack memory is fast as compared to heap memory.
8) Stack memory is used by  only threads whereas heap memory is used by whole application.
9) use stack when you know that how much memory is needed to allocate before compile time on the other hand, use heap if you are not sure about the amount of memory needed at runtime.

Related Posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • What is ThreadMXBean in java?
  • What is serialVersionUID
  • What is exchanger in concurrency?
  • UncaughtExceptionHandler in java

Filed Under: Core Java Tagged With: Difference between Stack and Heap memory, Heap memory, OutOfMemoryError, Stack memory, StackOverFlowError

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

  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?
  • What is deadlock in java?
  • Difference between Serializable and Externalizable

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

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

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.