|
Answer» The Stack memory in Java is used for thread execution. Specific values are stored in the stack memory that are available for a short time. Also, stack memory may contain data references to objects getting referred from the method that are in the heap memory. The order in Stack memory is Last In First Out (LIFO). A block is created in the stack memory for all the primitive values and references to other objects in a method when that method is invoked. After the end of the method, the memory block in the stack memory is free and can be used by another method. In general, the size of the stack memory is quite less as COMPARED to the heap memory. Some of the important FEATURES of Java Stack Memory are given as follows: - The stack memory can grow if more methods are called and it also shrinks if more methods are RETURNED.
- The error java.lang.StackOverFlowError is thrown if the stack memory is full.
- As long is the method with the variables is running, those variables exist inside the stack. After method is returned, the variables are deleted.
- The memory access time of stack memory is less than that of heap memory.
- The variables are automatically allocated and delocated in stack memory according to the method.
- Each thread has its own stack and so the stack memory is thread safe.
Some of the DIFFERENCES between stack memory and heap memory are given as follows: - The stack memory is only used by a thread whereas the heap memory is required by all the application parts.
- The stack memory management is done using LIFO but it is more complicated for heap memory as that is used by the whole application.
- The stack memory is comparatively short lived while the heap memory exists for the whole execution of the application.
- Stack memory is faster than heap memory as its memory allocation PROCEDURE is much more simple.
- The stack memory is only accessible by the individual thread but the heap memory objects can be accessed globally.
|