The Java virtual machine defines various runtime data areas that are used during execution of a program. Some of these data areas are created on Java virtual machine start-up and are destroyed only when the Java virtual machine exits. Other data areas are per thread. Per-thread data areas are created when a thread is created and destroyed when the thread exits
Stack
Stack is a memory place where the methods and the local variables are stored. Variable references (either primitive or object references) are stored in the Stack. Each thread has a private stack, which stores frames.
The following exceptional conditions are associated with Java virtual machine stacks
· If the computation in a thread requires a larger Java virtual machine stack than is permitted, the Java virtual machine throws a StackOverflowError.
· If Java virtual machine stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion, or if insufficient memory can be made available to create the initial Java virtual machine stack for a new thread, the Java virtual machine throws an OutOfMemoryError
Heap
Heap is a memory place where the objects and its instance variable are stored. Each time an object is created in Java it goes into the area of memory known as heap.
Information about HEAP
· The heap is created on virtual machine start-up.
· Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated.
· The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary.
· The memory for the heap does not need to be contiguous.
A Java virtual machine implementation may provide the programmer or the user control over the initial size of the heap, as well as, if the heap can be dynamically expanded or contracted, control over the maximum and minimum heap size.
The following exceptional condition is associated with the heap:
· If a computation requires more heap than can be made available by the automatic storage management system, the Java virtual machine throws an OutOfMemoryError
Heap and stack are the two main memories that JVM is concerned as far as memory allocation by the OS (operating system) is concerned. Stack and heap are the memories allocated by the OS to the JVM that runs in the system.
The primitive variables like int and double are allocated in the stack, if they are local method variables and in the heap if they are member variables (i.e. fields of a class). In Java methods local variables are pushed into stack when a method is invoked and stack pointer is decremented when a method call is completed. In a multi-threaded application each thread will have its own stack but will share the same heap. This is why care should be taken in your code to avoid any concurrent access issues in the heap space. The stack is 'threadsafe' (each thread will have its own stack) but the heap is not 'threadsafe' unless guarded with synchronization through your code.
Method Area
It is shared among all threads. It stores per-class structures such as the runtime constant pool, field and method code. It is logically part of the heap. Memory for class (static) variables and methods declared in the class is also taken from it.
Runtime Constant Pool Or Constant Pool
It is a per-class or per-interface runtime representation of the constant_pool table. The JVM maintains a per-type constant pool, including literals (string, integer, and floating point constants). Each runtime constant pool is allocated from the JVM method area.
Frame or Stack Frame
Frame holds local variables (including parameters), operand stack and partial results, and plays a part in method invocation and return. A new frame is created each time a method is invoked and is destroyed on method completion. Each frame has a reference to the runtime constant pool of the class of the current method.
PC Register
The Java virtual machine can support many threads of execution at once. Each Java virtual machine thread has its own pc (program counter) register. At any point, each Java virtual machine thread is executing the code of a single method, the current method for that thread. If that method is not native, the pc register contains the address of the Java virtual machine instruction currently being executed. If the method currently being executed by the thread is native, the value of the Java virtual machine's pc register is undefined. The Java virtual machine's pc register is wide enough to hold a returnAddress or a native pointer on the specific platform.
An Example –
class Memory {
static int x; /* static stack storage*/
public static void main(String[] args){
Memory memory = new Memory(); /* dynamic heap storage*/
int y =0; /* dynamic stack storage */
String myString = new String("Memory"); /* dynamic heap storage */
}
}
When you create an object using the new operator, for example memory = new Memory();, it allocates memory for the memory object on the heap. The stack memory space is used when you declare automatic variables.
Note, when you do a string initialization, for example String myString;, it is a reference to an object so it will be created using new and hence it will be placed on the heap.
Memory space for objects is always allocated in heap. Objects are placed on the heap. Built-in datatypes like int, double, float and parameters to methods are allocated on the stack.
where does static and final variables are located in memory i need brief explanation to this?
ReplyDeleteStatic methods are just methods, they are not stored on the heap, they just don't get to use a "this" parameter.
ReplyDeleteStatic variables serve as "roots" to the GC. As a result, unless you explicitly set them to null, they will live as long as the program lives, and so is everything reachable from them.
A situation is only considered a memory leak if you intend for the memory to become free and it doesn't become free. If you intend for your static variable to contain a reference to an object for part of the time, and you forget to set it to null when you're done with that object, you would likely end up with a leak. However, if you put it in the static variable and intend for it to be there for as long as the program is running, then it is most definitely not a leak, it is more likely a "permanent singleton". If the object got reclaimed while you wanted it to still exist, that would have been very bad.
As for your question about the heap: All objects in Java exist either on the heap or on the stack. Objects are created on the heap with the new operator. A reference is then attached to them. If the reference becomes null or falls out of scope (e.g., end of block), the GC realizes that there is no way to reach that object ever again and reclaims it. If your reference is in a static variable, it never falls out of scope but you can still set it to null or to another object.