Thursday, April 7, 2016

Java Heap and Stack Memory

Heap and Stack

Heap and Stack memory will be used to execute all java programs. 

1.Heap Memory

Heap memory will be used for the purpose of storing java objects. It will allocates some memory to your java objects.

We know about Garbage collector, its running on Heap memory and clean unwanted object reference.

Whatever object is created in Heap memory, it will become Global access. 

We can set the heap memory size by the command -Xms and -Xmx with JVM. Means,  start up and maximum size of heap memory.

Most of the time , we are getting java.lang.outOfMemoryError, (Heap space error) due to heap memory size full.

2. Stack Memory

Stack memory will be used mainly while execution of thread.

Stack memory is working with LIFO(Last-In-First-Out) concept.

All variables and methods will be created in stack memory and deallocate will happen automatically.

Its faster than Heap memory while allocation

Stack memory size is less compared to Heap memory

Once method execution was done, it will become unused and next method will be ready for execution

When stack memory is full, it will throw java.lang.stackOverFlow Error

Both Heap and Stack are stored in computer RAM.

No comments:

Post a Comment