Stack memory only stores local variables and references to objects.Īccess to stack memory is fast as compared to heap memory. Stack memory is small as compared to heap memory.Īll objects created during the application are stored in heap memory. That means it is used one at a time during thread execution. The application in parts uses stack memory. The entire application uses heap memory during its runtime. Here is a small comparison of stack and heap memory in java: Heap Memory
Access to stack memory is fast as compared to heap memory.Stack memory gets automatically allocated and deallocated after the method completes its execution.Stack Memory grows and shrinks as new methods are added and removed to stack memory.Here are some of the important features of stack memory. Therefore, the Stack memory size is less than heap memory. As soon as the execution of the method is completed, the block of memory in the stack becomes empty and used by the next method. Whenever a Java method is called, a new block is created in java stack memory to hold local or intermediate variables and references to other objects in the method. Stack memory is used for static memory allocation, and each executing thread in a java program has its stack memory.
The java runtime uses Heap Memory to allocate memory to objects and classes while executing a java program.
Web development, programming languages, Software testing & others Start Your Free Software Development Course