There are two types of memories available in JVM. One is Stack memory and other one is Heap memory.
Stack memory will store local variables and method calls.
One Stack Frame will create per every thread i.e per every method call in java stack memory and all local variables and method calls related data will be stored in Stack Frame. And also reference values of an object also will be stored in side Stack Frame.
So for every method call one stack frame will create and it will destroy the stack frame once method call is completed and here stack follows LIFO(Last In First Out) order.
So we will get StackOverFlowExcpetion if we try to run the method in infinitely As i shown @post StackOverFlow.
package com.sample.java.testing;
public class StackOverflowExceptionTest {
StackOverflowExceptionTest(){
new StackOverflowExceptionTest();
}
public static void main(String[] args) {
new StackOverflowExceptionTest();
}
}
We need to take care of methods should be end (not infinite loop) to avoid Stack over flow error.
Java heap memory will be stores object data. When ever we create object, it will store into Heap memory.
We will get out of memory error if we have large objects or referenced objects and if no space is available for JVM to allocate space for new objects which will store on Heap memory.
We need to do GC for un wanted objects to avoid this error.
We have options to increase the heap size:
-Xmx to set the maximum size for heap.
-xms to set minimum size for heap.
Default heap initial and maximum sizes will depend on physical memory of the system. Generally Heap initial size will be 1/64 th of physical memory. and Heap maximum size will be 1/4 the of physical memory or 1GB and half of the physical memory if physical memory is less than 192MB.
For example if machine has 128 MB of physical memory, then 64mb will be the maximum heap size and maximum heap size will be 256 MB if machine has 1 GB physical memory.
The maximum heap size will not used by JVM until unless we create more no objects.
These memory allocation will be same for both client JVM and server JVM.
Before J2SE5 default maximum size for heap is 64 MB and default initial heap size is 4MB
More details regarding heap and stack memory is available @Java Heap Vs Stack
Stack memory will store local variables and method calls.
One Stack Frame will create per every thread i.e per every method call in java stack memory and all local variables and method calls related data will be stored in Stack Frame. And also reference values of an object also will be stored in side Stack Frame.
So for every method call one stack frame will create and it will destroy the stack frame once method call is completed and here stack follows LIFO(Last In First Out) order.
So we will get StackOverFlowExcpetion if we try to run the method in infinitely As i shown @post StackOverFlow.
package com.sample.java.testing;
public class StackOverflowExceptionTest {
StackOverflowExceptionTest(){
new StackOverflowExceptionTest();
}
public static void main(String[] args) {
new StackOverflowExceptionTest();
}
}
We need to take care of methods should be end (not infinite loop) to avoid Stack over flow error.
Java heap memory will be stores object data. When ever we create object, it will store into Heap memory.
We will get out of memory error if we have large objects or referenced objects and if no space is available for JVM to allocate space for new objects which will store on Heap memory.
We need to do GC for un wanted objects to avoid this error.
We have options to increase the heap size:
-Xmx to set the maximum size for heap.
-xms to set minimum size for heap.
Default heap initial and maximum sizes will depend on physical memory of the system. Generally Heap initial size will be 1/64 th of physical memory. and Heap maximum size will be 1/4 the of physical memory or 1GB and half of the physical memory if physical memory is less than 192MB.
For example if machine has 128 MB of physical memory, then 64mb will be the maximum heap size and maximum heap size will be 256 MB if machine has 1 GB physical memory.
The maximum heap size will not used by JVM until unless we create more no objects.
These memory allocation will be same for both client JVM and server JVM.
Before J2SE5 default maximum size for heap is 64 MB and default initial heap size is 4MB
More details regarding heap and stack memory is available @Java Heap Vs Stack
No comments:
Post a Comment