Monday, 8 May 2017

Java Memory

Stack
  • There can be many stacks in JVM
  • Every thread has its own stack to store
    • Local variables(primitive and Object references)
    • method calls 
  • Data on STACK can only be seen by respective thread that owns it.
  • When the end of method is reached, stack variables are removed/popped out/destroyed.
Heap
  • One heap across ALL threads
  • All Objects are stored on HEAP(Class instances, Arrays,  Class Objects)
  • Allows us to store data that has longer life time than a single block or function.
    • eg: Objects that can be shared across methods
  • Value of primitive instance variable. 

Runtime Data Areas:

Method Area
  • Class level data/information
    • Meta infromation like
      • Names of type, superclass, super interfaces
      • Class or interface
      • type modifiers: abstract, final, public
    • Reference to class object
    • Field info
      • Name & type, 
      • Modifiers: static, final, access modifiers, transient, volatile
    • Filed type 
    • Value of primitive static variable. 
    • Runtime Constant pool
      • Literals
      • symbolic references
    • Method Info
      • Name
      • Return type
      • Number and type of arguments
      • Modifiers: static, final, access modifiers, abstract, synchronized, native
      • Method bytecode 
    • Method Table


NOTE: Prior to Java 8, method area was allocated in Permanent Generation(PermGen) space. It is completely removed in Java8. And method area has been moved to Native Heap. It is referred as Metaspace. Following are the reasons:
  • OutOfMemory is less likely as the metaspace has no max size for memory. System memory is limit. 
  • Improved GC process