Header Ads

Heap Concept Simplified In Java

When we call a java program to run, the JVM is invoked in the back end  It expects a class file to run. JVM is a program & it is calling a .class file to run. Both they need space in the main memory. How Java is going to handle this?

The memory management of Java interacts with operating system for space. For an operating system , JVM is a process which gets registered in task manager. Operating system allocates some memory space to JVM. But note that Java API could not share two memory locations.


Pics



Basically:- 

In Java memory allocation happens for object. Even array is treated as an object in Java. JVM internally divided this space into three segments:
1. Heap
2.    Stack
3.    All Source Codes

Heap:- A place where all string,objects,variables get stored.

Stack:-The structure of a program.
All Source Codes: - The source code for JVM along with garbage collector, our program.



Class ABC {

public static void main (String args[]){
    Int a;
   Int b;
 Void set data()
}
public void set data(int a,int b)
{
this.a=a;
this.b=b;
}




Pics


There are two sizes of objects that matters when allocating memory:

1. Shallow Size:-The amount of memory allocated to store an object it self. It depends on-
Number
Types of used field
For array it depends on array length, types of objects it stored.
2. Retained Size:-Shallow size of the parent object & the child objects (directly or indirectly) accessible from this objects.

The heap size depends on the processor to process ,OS to OS.

A 32 bit operating system starts from 3670k to 64M. A new programmer need not bother about the heap size of Java, but advanced programmer may increase by Java command line.
1.-xmr-Starting size of heap
2.-xmx-Maximum size of the heap



JVM option passed Meaning
-Xmsinitial java heap size
-Xmxmaximum java heap size
-Xmnthe size of the heap for the young generation

If you know your program is long enough & creates a lot of objects then it is recommended to increase heap size.


How Heap Works:-

Heap is internally divided into three parts
New or Young Generation
Old Or Tenured Generation
Perm or Permanent Area
Pics


An object when created fresh, assigned to heap under eden space. After subsequent sweap of minor garbage collection it moved to survivor 2 states. Most of the objects become dead here.Permanent generation of heap is special case where meta data,string pools resides.For this cases all the reference got disconnected from object.

Below are the heap based errors:-

I. Java.lang.Outofmemory error heap space if heap does not have space
II. Java.lang.Outof memory error perm Gen.space if a non regular heap area in Hotspot  VM is out of space.
iii.     java.lang.Outofmemory error:request<size> byte for<reason> if allocation to the native heap Fails


Benefits of Memory Heap In Java:-

I. Pre allocated large blocks
II. Allocation technique-allocating in small block is very fast
III. No need to roam for free memory in main memory.
IV. No fragmentation required
V. Continuous memory block
VI. Exception handling.

Powered by Blogger.