Java Heap Space:
Java Heap space is used to store objects. When ever we create object for class, it will always store in heap memory.
Garbage collector(GC) will run on heap space to clean up memory for un necessary objects. GC free memory for an object if it does not have any reference.
Objects created in heap space have global access and can be accessible anywhere from application.
Stack Memory:
Stack memory will create per every thread and local variables, method calls and reference of objects will be stored in stack memory.
Stack memory will follow Last In First Out(LIFO) order.
New block will create in stack memory for every method call to store local primitive variables and any reference to other objects which are being used by the method.
This new block will be free and available for next method once method execution is completed.
Stack memory size is less when compared to Heap memory size.
Java Program to explain the Heap and Stack memory:
package com.sample.javase.testing;
public class JavaMemoryTest {
public void setValue(String value) {
String name = value;
System.out.println(name);
}
public static void main(String[] args) {
int a = 20;
JavaMemoryTest javaMemoryTest = new JavaMemoryTest();
javaMemoryTest.setValue("java");
}
}
Heap Vs Stack memory
1. Object created will always stored in heap memory and only reference of it will stored in Stack memory.
2. Stack memory is per thread i.e Stack memory can not accessible by other threads. Where as heap memory is used by application i.e objects stored in heap memory are globally accessible.
3.Stack follows LIFO for memory management but memory management is bit complex in heap as it is globally accessible. Heap memory further will dived into Young Generation , Old Generation etc.
4. Stack memory is short lived and heap memory is live till end of application.
5. We can set the Stack memory size by using -Xss JVM option and we can use -Xms and -Xmx for heap start up size and maximum size.
6. Stack memory is less when compared to heap.
7. We will get StackOverflow Exception if Stack memory is full and OutOfMemoryError if heap size is full.
Java Heap space is used to store objects. When ever we create object for class, it will always store in heap memory.
Garbage collector(GC) will run on heap space to clean up memory for un necessary objects. GC free memory for an object if it does not have any reference.
Objects created in heap space have global access and can be accessible anywhere from application.
Stack Memory:
Stack memory will create per every thread and local variables, method calls and reference of objects will be stored in stack memory.
Stack memory will follow Last In First Out(LIFO) order.
New block will create in stack memory for every method call to store local primitive variables and any reference to other objects which are being used by the method.
This new block will be free and available for next method once method execution is completed.
Stack memory size is less when compared to Heap memory size.
Java Program to explain the Heap and Stack memory:
package com.sample.javase.testing;
public class JavaMemoryTest {
public void setValue(String value) {
String name = value;
System.out.println(name);
}
public static void main(String[] args) {
int a = 20;
JavaMemoryTest javaMemoryTest = new JavaMemoryTest();
javaMemoryTest.setValue("java");
}
}
Heap Vs Stack memory
1. Object created will always stored in heap memory and only reference of it will stored in Stack memory.
2. Stack memory is per thread i.e Stack memory can not accessible by other threads. Where as heap memory is used by application i.e objects stored in heap memory are globally accessible.
3.Stack follows LIFO for memory management but memory management is bit complex in heap as it is globally accessible. Heap memory further will dived into Young Generation , Old Generation etc.
4. Stack memory is short lived and heap memory is live till end of application.
5. We can set the Stack memory size by using -Xss JVM option and we can use -Xms and -Xmx for heap start up size and maximum size.
6. Stack memory is less when compared to heap.
7. We will get StackOverflow Exception if Stack memory is full and OutOfMemoryError if heap size is full.
No comments:
Post a Comment