As we know JVM stores Class objects, Static variables to heap memory location and Local variables and calls to methods to stack. Why so? why can't we use a single memory type to store everything? What is the need of having two type of memory location, Any advantage?
-
1Because the stack is a FIFO that corresponds to the memory used by methods, so it gets automatically released. You could use the heap for everything but it would just be more work.user207421– user2074212014-03-14 10:05:16 +00:00Commented Mar 14, 2014 at 10:05
-
This is not unique to JVM. Storing stuff with a predefined lifetime (local variables, method calls etc) in the stack is far more efficient than storing them in the heap.kviiri– kviiri2014-03-14 10:06:21 +00:00Commented Mar 14, 2014 at 10:06
-
@EJP: You mean LIFO, right?meriton– meriton2014-03-14 10:13:53 +00:00Commented Mar 14, 2014 at 10:13
-
So when we talk about the memory management in java that all related to heap memory. How come GC comes to know that yeah it needs to free particular memory?786543214– 7865432142014-03-14 10:18:43 +00:00Commented Mar 14, 2014 at 10:18
3 Answers
The great thing about the stack is that allocation and cleanup are so quick and easy. That's why it's used for locals.
When the JVM calls a function, it leaves a block of room on the stack for the function's locals by moving the stack pointer down (or up, whatever). When the function exits, all it has to do to "clean up" the stack is move the pointer back up. Very fast, and doesn't lead to memory segmentation.
A slightly visual example:
Suppose we have this stack
+----------------+ | being used | | being used | | |<-- stack pointer | | | | | | | | +----------------+
Then we call a function that needs some locals, so we move the stack pointer and use that part of the stack for the locals:
+----------------+ | being used | | being used | | locals | | locals | | locals | | |<-- stack pointer | | +----------------+
Now the function exits; we just move the stack pointer back:
+----------------+ | being used | | being used |<-- stack pointer | defunct locals | | defunct locals | | defunct locals | | | | | +----------------+
Comments
Allocation on a stack is cheaper, but assumes that memory is freed in reverse order of allocation.
Local variables live during a method execution - and methods complete execution in reverse order of their invocation. Therefore, it is trivial to use a stack for holding local variables.
Objects live as long as they reachable, which in general is impossible to predict. Therefore, they can not (in general) be allocated on the stack. However, if the JVM can determine (through escape analysis) that an object does not escape a particular method invocation, it can put it on the stack instead.
Comments
When we have a declaration of the form “int a;”:
a variable with identifier “a” and some memory allocated to it is created in the stack.
The attributes of “a” are:
Name: a
Data type: int
Scope: visible only inside the function it is defined, disappears once we exit the
function.
Address: address of the memory location reserved for it.
Note: Memory is allocated in the stack for a even before it is initialized.
Size: typically 4 bytes
Value: Will be set once the variable is initialized
Since the memory allocated for the variable is set in the beginning itself,
we cannot use the stack in cases where the amount of memory required is not known
in advance. This motivates the need for HEAP
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.