Wednesday, July 23, 2014

Stack memory in Java ..How to release memory of stack ....



·         Each thread has its own stack when ever thread initialize then that thread gowns to memory of stack
·         If there is no memory left in stack for storing function call or local variable, JVM will throw
·         Exceptions of stack
 java.lang.StackOverFlowError, while if there is no more heap space for creating object, JVM will
 throw
java.lang.OutOfMemoryError: Java Heap Space. Read more about how to deal with c:- java.lang.OutOfMemoryError

A) 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 lot lesser than size of  heap memory in Java.

B) 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

·         Heap and stack both store in RAM
·       Can an object be stored on the stack instead of the heap?
·         Yes, an object can be stored on the stack. If you create an object inside a function without using the “new” operator then this will create and store the object on the stack, and not on the heap

How long does memory on the stack last versus memory on the heap?

Once a function call runs to completion, any data on the stack created specifically for that function call will automatically be deleted. Any data on the heap will remain there until it’s manually deleted by the programmer.
·         We cannot increase the size of stack but we can increase the size of heap
·         By the help of commands
·         Xss> increase to stack
·         Xms>
·         Xmx> increase to heap memory.
Both of them initialized memory at dynamic time mostly it can static also

Which is faster – the stack or the heap? And why?

The stack is much faster than the heap. This is because of the way that memory is allocated on the stack. Allocating memory on the stack is as simple as moving the stack pointer up.

How is memory deallocated on the stack and heap?


Data on the stack is automatically deal located when variables go out of scope.

free, delete, or delete[ ]. Other languages like Java and .NET use garbage collection to automatically delete memory from the heap, without the programmer having to do anything..

What can go wrong with the stack and the heap?

If the stack runs out of memory, then this is called a stack overflow – and could cause the program to crash. The heap could have the problem of fragmentation, which occurs when the available memory on the heap is being stored as noncontiguous (or disconnected) blocks – because used blocks of memory are in between the unused memory blocks. When excessive fragmentation occurs, allocating new memory may be impossible because of the fact that even though there is enough memory for the desired allocation, there may not be enough memory in one big block for the desired amount of memory.

Which one should I use – the stack or the heap?

For people new to programming, it’s probably a good idea to use the stack since it’s easier. Because the stack is small, you would want to use it when you know exactly how much memory you will need for your data, or if you know the size of your data is very small. It’s better to use the heap when you know that you will need a lot of memory for your data, or you just are not sure how much memory you will need (like with a dynamic array).

No comments:

Post a Comment