Showing posts with label Stack. Show all posts
Showing posts with label Stack. Show all posts

Friday, 3 July 2015

Error messages related to Memory Exhausted ( Garbage Collector ) in Java

➟ Purpose

      The main purpose of this article is to understand the root cause behind different kind of error messages we generally face while our day to day coding life. Here, I am going to clear some important and most seen error messages.

➟ Class Hierarchy for Error

➟ Some Examples

➤ java.lang.OutOfMemoryError: GC Overhead limit exceeded

• It does not indicate that heap space memory is 100% full, but it's very small.
• If more than 98% of the total time is spent in garbage collection and less than 2% of the heap is recovered, then this kind of error is thrown.
• This feature is designed to prevent applications from running for an extended period of time while making little or no progress because the heap is too small.
• It can be disable using -XX:-UseGCOverheadLimit

➤ java.lang.OutOfMemoryError: Requested array size exceeds VM limit

• The application (or APIs used by that application) attempted to allocate an array that is larger than the heap size.
• For example, if an application attempts to allocate an array of 512 MB but the maximum heap size is 256 MB.

➤ java.lang.OutOfMemoryError: Java heap space

• Heap space memory is 100% full or it is less than required for allocating to new object.
• This error does not necessarily always imply a memory leak, but it can be.
• The problem can be as simple as a configuration issue, where the specified heap size (or the default size, if it is not specified) is insufficient for the application.

➤ java.lang.OutOfMemoryError: PermGen space

• It indicates the perm gen memory (subpart of heap) is full.
• It indicates it's not able to store more class or method metadata.

• It will be no more thrown while using JDK 8.

➤ java.lang.OutOfMemoryError: Metaspace

• It indicates the native memory (referred to here as metaspace) is full.
• It indicates it's not able to store more class or method metadata.
• It can be thrown while using JDK 8.

➤ java.lang.StackOverflowError

• It indicates the perm gen memory (subpart of heap) is full.
• It indicates it's not able to store more class or method metadata.


Wednesday, 1 July 2015

Heap vs Stack Memory Java

Stack Memory Heap Memory
What? It's type of temporary memory used by JVM to store local method primitives variables, and method calls information. It's type of memory used by JVM to store references of objects (regardless where they are created) and String Pool.
Size? Small, It's very less compare to Heap. Big, It's very more compare to Heap.
Scope? Single Thread Multiple Threads
Shared? No, It is not shared between multiple threads. Each thread will have it's own stack memory. Yes, It is shared between multiple threads, so each thread can get reference of any object.
Example? Infinite Recursion :
public static void test(long a,long b){
   test(a++,b++);
}
Adding Infinite String in List/Set.
public static void test(){
      List list = new ArrayList();
      while(true){
          list.add(new String("Test"));
      }
}
Notification of Exceed Memory JVM will throw StackOverflowError JVM will throw OutOfMemoryError
Access Spped? Fast, Single thread will access it, so obviously it can be access more faster compare to Heap. Slow, Multiple thread will access it, so obviously it can be access slower compare to Stack.
How to Apply? JVM Option -Xss=10M, It's vary from version to version. by default 128KB for windows 64 bit. Oracle Docs JVM Option -Xms=512M (Initial Java heap size) and -Xmx=1024M (Maximum Java heap size)
How to Avoid? Try to avoid recursive code as much as possible. Recursion code can be always tranform into Interative.

Why? : Recursion vs Iteration
How? : Binary Search Alorithm
Make sure that any objects have not live/strong references behind your eyes which leads to Memory Leak.

Avoid open Streams, close them. (mostly with IO)
Avoid open connections, close them. (mostly with database operations)
Avoid new String(String s), alternative use new String().intern()
Garbage collected? No, Once a function call runs to completion, any data on the stack created specifically for that function call will automatically be deleted. Yes, Once all references for any object are null, it will be collected by GC which is run by JVM.
Common Points? Stack and Heap both are stored in the computer's RAM.