Java Memory Model

1 Nov

One of the biggest strength of the java platform is its automatic memory allocation in JVM. The developer need not worry about the complexity of memory allocation and garbage collection.
Java stores objects in Heap Memory. JVM Memory is internally partitioned into three different areas. These areas are called Generations.
We have three types of generations
1. The Young Generation (Part of Heap)
2. The Tenured Generation (Part of Heap)
3. The Permanent Generation (Not Part of Heap)

The young and tenured Generation spaces are used for allocating the memory to objects.
The permanent Generations is used to hold the data needed by the JVM to describe objects. The permanent Generation will store the internal representation of the Classes.
Out if these three partitions, only the young and tenured generation spaces are used for allocating memory to objects. The permanent generation space is used for holding the data needed by the virtual machine to describe objects – internal representation of classes and other details required by the JVM and JIT.
This partitioning is done in order to improve the performance of garbage collection as performing garbage collection on the entire heap will be very expensive. Instead the architects of java decided to partition the heap space into three parts.
The Garbage collection happening in Young Generation is called Minor Collection
The Garbage collection happening in Tenured Generation is called Major Collection
The Garbage collection doesn’t happen in the Permanent Generation.

The Young Generation
The young generation is where space is allocated for newly created objects. In most applications, most of the objects created are used and referenced only for a very short span of time, for example an iterator instance is discarded as soon as the loop is complete.
The purpose of having a separate space of young generation is to maximize promptness (the time between when an object becomes dead and when the memory becomes available) and to reduce the overhead of scanning the entire heap during every garbage collection process.
Garbage collection in the young generation happens when the space in this generation fills up and is called the minor collection. Minor collections can be optimized assuming a high infant mortality rate. It is well-tuned in the sense that the young generation is large enough (and thus the period between minor collections long enough) that the minor collection can take advantage of the high infant mortality rate. The time required for garbage collection is directly proportional to the number of live objects. A young generation full of dead objects is collected very quickly. Some surviving objects are moved to a tenured generation.
The Tenured Generation
Objects which survive the young generation are eventually moved into the tenured generation. Garbage collection in the tenured generation happens when the space in the tenured generation fills up and is called the major collection. Since the tenured generation usually have a large number of alive objects, the time required for garbage collecting the tenured generation is much higher than that for the young generation. Hence, garbage collection of the tenured generation is done at a much lesser rate than the young generation.
The Permanent Generation
The permanent generation is not used for allocating objects. Instead it is used for holding the data needed by the virtual machine to describe objects that do not have equivalence at the Java language level. For example objects describing classes and methods are stored in the permanent generation. The permanent Generation is not garbage collected.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: