Showing posts with label Heap. Show all posts
Showing posts with label Heap. Show all posts

Friday, 3 July 2015

Memory types and Garbage Collector

➟ Purpose

      The main purpose of this article is to understand the structure of heap memory and how Garbage Collector works with it.

➟ Logical Structure of Memory (Image Source)



➟ Heap Memory

• The heap is the memory where all your objects will be stored. • The heap is created when the JVM starts up and may increase or decrease in size while the application runs.
• When the heap becomes full, garbage is collected. • We can define initial heap size using -Xms and maximum heap size using -Xmx.

➟ Areas (or Generations)

➤ New/Young/Nursery Generation(Space)

  • It's logical part of heap memory which is reserved for allocation of new objects.
  • When it becomes full, garbage is collected by running a special "young collection".
  • It can be define using -Xmn=512M

    ➤ Survivor1(From)

      • It's logical part of Young memory.
      • If object live after some fix amount of GC cycles, it will be move here from Eden space while performing Minor GC.

    ➤ Survivor2(From)

      • It's logical part of Young memory.
      • If object live after some fix amount of GC cycles, it will be move here from Survivor2 while performing Minor GC.

  • We can define the ratio between Eden space and survivor space using -XX:SurvivorRatio. For example if Young Generation size is 12m and VM switch is -XX:SurvivorRatio=2 then 6m will be reserved for Eden Space and 3m each for both the Survivor spaces. The default value is 8.

➤ Old/Tenured Generation(Space)

  • Object will be moved/promoted here when they are long live objects.
  • Object will be moved here while Major GC cycle.
  • The size of this space can be calculated as : Total Heap size - -Xmn (used for Old space).
  • We can also define ratio between old space and new space using -XX:NewRatio. for example, -XX:NewRatio=3 which is equivalent to Young:Tenured <=> 1:3 ratio.

➤ Perm Gen (The Permanent Generation) (before jdk 1.8)

  • It consists mostly of class declarations loaded and stored into it.
  • It stores Class and Methods' metadata like the name and fields of the class, methods with the method bytecode.
  • It stores Constant Pool information, object arrays and type arrays associated with a class and Just In Time compiler optimizations.
  • It stores String Pool information before jdk 7.
  • It can be specified using -XX:PermSize=256m (initial size) and -XX:MaxPermSize=356m

➟ Some Definitions/Terminologies

Major GC : Major GC is cleaning the Old/Tenured space.
Minor GC : Major GC is cleaning the Young(Eden and Survivor spaces) space.
Full GC : Full GC is cleaning the entire Heap – both Young and Old/Tenured spaces.
Application Pauses : While running GC, some application threads will pause, it's called Application Pauses.

➟ Java 8 Updates

➤ PermGen bybye..!, Welcome MetaSpace :)

  • The metadata has now moved to native memory to an area known as the “Metaspace”.
  • No longer have the PermGen, is not gone, so no more java.lang.OutOfMemoryError: PermGen space.
  • Similarly Metaspace introduced, so from now java.lang.OutOfMemoryError: Metaspace will be there, but it will be rare as Metaspace increase dynamically.
  • PermGen related JVM options will be just ignored
  • MetaSpace increases dynamically as needed, however MaxMetaspaceSize is available in case we want to limit it.
  • Slow compare to PermGen. (obviously, to get something, always need to pay something..!!)
  • More details : Reference

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.