Saturday, May 21, 2011

Memory Management in java

Memory management is the process of recognizing when allocated objects are no longer needed, deallocating (freeing) the memory used by such objects, and making it available for subsequent allocations. Garbage collection solves many, but not all, memory allocation problems. You could, for example, create objects indefinitely and continue referencing them until there is no more memory available. Garbage collection is also a complex task taking time and resources of its own.



===========================
When does GarbageCollector will run
What will happen if garbage collector is not able to free memory
===========================
Consider the possibility that the garbage collector may never even run during an application's lifetime. There is no guarantee as to when or if the JVM will invoke the garbage collector -- even if a program explicitly callsSystem.gc(). Typically, the garbage collector won't be automatically run until a program needs more memory than is currently available. At this point, the JVM will first attempt to make more memory available by invoking the garbage collector. If this attempt still doesn't free enough resources, then the JVM will obtain more memory from the operating system until it finally reaches the maximum allowed.
 Garbage Collection Algorithms
1 – Reference Counting:
2 – Tracing Collectors:
3 – Mark-Sweep collectors:
4 – Copying Collectors:
5 – Heap Compaction:
6 – Mark-compact collectors:
JDK uses all of the algorithms in some sense. Early JDK used mark-sweep and mark-compact while version 1.2 and later employed a hybrid approach.
 
An object is created in the heap and is garbage-collected after there are no more references to it. 
The sequence of the garbage collection process is as follows:

1. Root set tracing and figure out objects that are not referenced at all.
2. Put the garbage objects from above in finalizer Q
3. Run finalize() of each of these instances
4. Free memory

The JavaHeap is separated into three regions
1. New Generation objects
2. Old Generation objects
3. Permenent Generation objects
The New Object Regions is subdivided into three smaller regions:
 1. Eden , where objects are allocated
 2. Survivor semi-spaces:  From
3. Survivor semi-spaces:  To
When the Eden area is full, the GC does a reachability test and then copies all the live objects from Eden to the To region. objects get created in New generation and then move to survivor Spaces at every GC run, and if they survive for long to be considered old, they get moved to the Tenured generation
By default, Java has 2 separate  threads for GC, one each for young(minor GC) and old generation(major GC). When ever there is requirment for memory then, first minor GC will runs if it could not release memory, then major Gc thread will run, even it could not release memory then JVM increase heap memory. This cycle run untill current memory reaches the MaxMemory for the JVM (default is 64MB for client JVM), after which JVM throws OutOfMemory Error.

Permanent Generation
Class information is stored in the perm generation. Also constant strings are stored there. Strings created dynamically in your application with String.intern() will also be stored in the perm generation.Reflective objects (classes, methods, etc.) are stored in perm. It holds all of the reflective data for the JVM

No comments:

Post a Comment