Pages

Thursday 10 October 2013

Java HEAP in Deep and dont Sleep!!


When I started java programming I didnt know what is Heap? I started learning Java concepts without knowing where its all getting saved in memory.
Its happens with most of programmer because learning language is easy but learning basics is difficult since there is no formal process which can teach you every basics of programming its experience and work which reveals the secret of programming.This post is mainly for beginners who are learning java. One thing I would suggest is first learn java concepts after that dig deeply about where its getting saved. If you started with heap you will get so many doubts about concepts. For Example, you should know about objects concept before learning "objects will be saved in heap memory" else you wont understand exact essence of object in heap.

Here are the some important things of heap that I have shared with you. Please refer other refference also.

When Java program started JVM (Hope, you know what is JVM)gets some memory from Operating system and part of that memory is called HEAP.JVM will allocate heap memory by Xms command which will tell the size required for heap. Default size of heap memory will be changed based on the bit and OS.

If heap size is not sufficient then JVM will set maximum heap memory by -xmx command.

Eg : -xmx512m. Be careful in giving "M"or "G". Single alphabet can change anything :p

When your application is ready for executing, Analyse how much heap memory is needed by your application. Then, execute the command -xms/xmx before JVM starts. Once JVM is started you cant reallocate your application heap memory.

All your objects will be saved inside heap memory. Memory is divided into 3 parts.
1) Old generated (Heap Memory)
2) New generated(Heap Memory)
3) Perm Space(Non Heap Memory)

Old Generated: Many objects will be created and saved inside the heap. Many objects might have been killed by Garbage collector. Remaining objects which are all not killed by garbage collector will be accumlated in old generated heap memory.

New Generated: Newly created object will be saved in new generated heap memory.

Perm space: Meta data about classess, method, string constant pool etc other than objects will be saved in perm space.

outofMemoryError: If your application tries to create an object and finds that there is no enough heap memory to accomadate. So, Garbage collector will run and it will free the memory of objects which are all not referenced to any memory. Objects will be created in the new generated heap memory. Though garbage collector tries to free memory , it couldnt find non referenced object. In such cases, application will throw an error called outofmemory error. Application needs some heap memory and heap memory is not enough to accomdate all those objects.

outofmemoryerror:permspace : The classloader is responsible for loading all classes in perm space. Classes will be unloaded when classloader is attacked by garbage collector. Suppose an object retain its reference the classloader which is attacked by garbage collector then it will lead to MEMEORY LEAK and shows outofmemoryerror:permspace error.

You can analyse your java heap by using some tools which is availble inside JDK. You can take snapshot of java heap at particular time called java heap dump and you can analyse it.Java heap dump will be created by using the command "jmap"."jhat: Java heap analyser tool" is available to anlayse the java heap and make use of it :)

You can also find the information of your application regarding memory during runtime by using following commands.
Runtime.totalmemory() : tells the totalmemory occupied by your application
Runtime.freememory(): tells how much free memory is availble
Runtime.MaxMemory(): tells maximum available for your application.

Use Profiler and Heap dump Analyzer tool to understand Java Heap space and how much memory is allocated to each object.

I hope the above information will be useful atleast for some extent :) Happy coding :)

No comments:

Post a Comment