JAVA Memory Management - Smart Tech Guides

Latest

Hi this is Sravan Kumar from India. SMART TECH GUIDES is a technical blog. it helps you to learn about Java topics, frameworks and tools like Jenkins, GitHub & related explanations, data base related topics. All these Explanations are explained in simple and understandable manner.

Subscribe Us

SMART TECH GUIDES

JAVA Memory Management

JAVA Memory Management:

 

Java virtual machine(JVM) is used to execute Java byte code (.class files) and converts byte code into machine understandable language i.e.., binary values(0's, 1's).

 

Java virtual machine loads the code, verifies byte code instructions and managing objects(allocating and deallocating) memory for our program.


JVM is divided into five different parts


1)Method Area

2)Heap Area

3)Stack Memory

4)Pc registers

5)Native method stacks


 

Jvm achitecture



Method Area:

when JVM start up method area used to store our class structure, super class name and type of modifiers, class code, static variables and static and non-static methods etc.

 

Stack Memory

Stack memory Stores only primitive variables and reference to objects that are created in heap space.

Stack memory is allocated and deallocated when methods called and returned respectively. 

each thread separate stack will be created.


Java stack area combination of stacks, every stack has stack frames.

and each stack frame divided into 3 sub parts.

1)Local variable array

2)Operand stack

3)Frame data. (Stores exception data)


Stack follows LIFO (last in first order).

Stack size is very small.

default size for each stack around 1mb depends on OS(operating system).

we can increase size of the stack.

If we want to set size of the stack equal to 1mb by using this command "java -xss1m".

and also we can set size in kb's i.e., "java -xss1024k".

 

Heap Area:

when you use new key word the JVM creates instance for object in a heap, while the reference of that object stores in stack. and also, non-static variables available when object is created.

each object maintains separate storage memory but it does not maintain storage order.

Heap space is allocated when new object is created and deallocated by garbage collector

Heap size is bit high compared to stack

We can increase and decrease size of the heap by

Java -xmx(req size)  and java -xms(req size)

 

Pc registers

Pc register contains the memory address of the instruction of the method.

the Java virtual machine can support many threads of execution at once.

each JVM thread has its own pc (program counter) register.

Pc register is created every time when a thread is created.

once execution finish pc counter also vanished.

 

Native method stacks

when a thread invokes Java method JVM creates stack memory and push on to the Java stack.

but when a thread invokes a native method Java native interface (JNI) calls native stack. 

Note: here native methods are written in other programming language other than Java.

Similar to Java stack, native methods are executed on the native method stacks.

No comments:

Post a Comment