Wednesday, April 8, 2009

Heap Memory and Stack Memory in Java

Difference between Heap Memory and Stack Memory in Java:

STACK memory is referred as temporary memory,if you come out of the program the memory of the variable will not no more there. STACK memory and HEAP memory may be allocated by the compiler for data storage. The stack is where memory is allocated for automatic variables within functions. A stack is a Last In First Out (LIFO) storage device where new storage is allocated and de-allocated at only one 'end' called the Top of the stack.

HEAP memory is referred as permanent memory,memory allocated for the object will be maintained even if we came out of the program. For example memory for OBJECT will remains there ever.

The heap segment provides more stable storage of data for a program memory allocated in the heap remains in existence for the duration of a program. Therefore, global variables (storage class external), and static variables are allocated on the heap. The memory allocated in the heap area, if initialized to zero at program start, remains zero until the program makes use of it. Thus, the heap area need not contain garbage.

Comparison:

When a function or a method calls another function which in turns calls another function etc., the execution of all those functions remains suspended until the very last function returns its value. This chain of suspended function calls is the stack, because elements in the stack (function calls) depend on each other. The stack is important to consider in exception handling and thread executions.

The heap is simply the memory used by programs to store variables. Element of the heap (variables) have no dependencies with each other and can always be accessed randomly at any time.

0 comments:

Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP