-
Notifications
You must be signed in to change notification settings - Fork 55
/
Stack vs Heap memory in Java
66 lines (47 loc) · 3.77 KB
/
Stack vs Heap memory in Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
http://javarevisited.blogspot.com/2013/01/difference-between-stack-and-heap-java.html
IMP Q: Does each thread in java uses seperate stack?
Ans: In Java every thread maintains its own separate stack. It is called Runtime Stack but
they share the same memory.
Difference between stack and heap memory is common programming question asked by beginners learning
Java or any other programming language. Stack and heap memory are two terms programmers starts hearing
once they started programming but without any clear and definite explanation. Lack of knowledge on what
is heap in Java and what is stack memory in Java, results in misconcepts related to stack and heap.
To add to this confusion, stack is also a data structure which is used to store elements in LIFO
(Last In First out) order and available in Java API as java.util.Stack. In general both stack and heap
are part of MAIN MEMORY, a program is allocated and used for different purposes. Java program runs inside JVM
which is launched as a process by "java" command. Java also uses both stack and heap memory for
different needs. In our last article 10 points on Java heap space I have touched base on Java heap
space and in this article we will see difference between stack and heap memory in Java.
Difference between Stack vs Heap in Java:
Difference between heap and statck in Java - Interview Question
Here are few differences between stack and heap memory in Java:
1) Main difference between heap and stack is that stack memory is used to store local variables
and function call, while heap memory is used to store objects in Java. No matter, where object is
created in code e.g. as member variable, local variable of any method either main method or different method
or objects creates as a class variable, they are always created inside heap space in Java.
2) Each Thread in Java has there own stack which can be specified using -Xss JVM parameter, similarly
you can also specify heap size of Java program using JVM option -Xms and -Xmx where -Xms is starting
size of heap and -Xmx is maximum size of java heap. to learn more about JVM options
see my post 10 JVM option Java programmer should know.
3) If there is no memory left in stack for storing function call or local variable,
JVM will throw java.lang.StackOverFlowError, while if there is no more heap memory for creating object,
JVM will throw java.lang.OutOfMemoryError: Java Heap Space. Read more about how to deal
with java.lang.OutOfMemoryError in my post 2 ways to solve OutOfMemoryError in Java.
4) If you are using Recursion, on which method calls itself, You can quickly fill up stack memory.
Another difference between stack and heap is that size of stack memory is VERY LESSER(VERY LESS) than size of
heap memory in Java.
5) Variables stored in stacks are only visible to the owner Thread, while objects created in
heap are visible to all thread. In other words stack memory is kind of private memory of Java Threads,
while heap memory is shared among all threads.
VERY IMPORTANT:
Difference between heap and string pool memory: (Source: http://www.coderanch.com/t/390447/java/java/Difference-Heap-Pool)
Video of 6 minutes to explain this: https://www.youtube.com/watch?v=WTOiuY7cNTo
Heap:
The heap is just the area of RAM that is allotted to the JVM by the operating system for runtime activity.
As such it is the place that objects get created during a JVM session.
Pool:
Pool is a separate portion WITHIN the heap memory. This separate portion of heap memory is called "POOL"
The 'pool' is just a special area of the heap memory that Java sets up to store all String literals.
Imagine a box with a partition wall; on one side is the 'pool' on the other side the 'heap'.
Everytime you use a string literal, it gets stored on the pool side, regular objects get stored on
the heap side.