786543214
786543214

Reputation: 855

Why JVM uses heap for objects and static variables and Stack for local variables and function call?

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?

Upvotes: 0

Views: 371

Answers (3)

Arjun Chaudhary
Arjun Chaudhary

Reputation: 2453

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.

Upvotes: 0

meriton
meriton

Reputation: 70564

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.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074048

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 |
|                |
|                |
+----------------+

Upvotes: 1

Related Questions