Monday, November 5, 2012

Stack & Heap

The JVM divided the memory into following sections.
  1. Heap
  2. Stack
  3. Code
  4. Static
This division of memory is required for its effective management.

Heap and stack - what is it?
The code section contains your bytecode.
The Stack section of memory contains methods (be called), local variables.
The Heap section contains Objects and their Instance Variables.
The Static section contains Static data/methods.
Of all of the above 4 sections, you need to understand the allocation of memory in Stack & Heap the most, since it will affect your programming efforts


Points to Remember:
  • When a method is called , a frame is created on the top of stack.
  • Once a method has completed execution , flow of control returns to the calling method and its corresponding stack frame is flushed.
  • Local variables are created in the stack
  • Instance variables are created in the heap & are part of the object they belong to.
  • Reference variables are created in the stack.



































Local Variables -> Stack
Instance Variables (Member Variable) -> Heap
(No matter it s a primitive variable or reference variable)
Stack and Heap memory

No comments:

Post a Comment