1.

Heap Space in Java

Answer»

The Heap Space in Java is allocated the memory to the Objects and the JRE classes by the Java runtime. All the objects in the application are created in the heap space. The objects in the heap space are GLOBALLY accessible from any place in the application and so they have a lifetime for the whole application execution.

The Memory Model of the heap space is divided into parts known as generations. Details about these are given as follows:

1. Young Generation

All the new objects are allocated in the young generation and they age here. When this place fills up, then minor garbage collection occurs.

2. Old Generation

All the longer existing objects are stored in the old generation. When objects in the young generation reach a certain age threshold, they are MOVED to the old generation.

3. Permanent Generation

The Java metadata for runtime classes is stored in the permanent generation,.

Some of the important features of Heap space in Java are given as follows:

  1. The heap space contains the young generation, old generation and the permanent generation.
  2. The stack memory is faster than the heap space in Java.
  3. The error java.lang.OutOfMemoryError is thrown if the heap space is full.
  4. The heap space is not thread safe. It needs SYNCHRONIZATION methods for safety purposes

Some of the differences between stack memory and heap memory are given as follows:

  1. Stack memory is faster than heap memory as its memory allocation procedure is much more simple.
  2. The stack memory is only used by a thread whereas the heap memory is required by all the application parts.
  3. The stack memory is only accessible by the individual thread but the heap memory objects can be accessed globally.
  4. The stack memory is comparatively SHORT LIVED while the heap memory exists for the whole execution of the application.
  5. The stack memory management is done using LIFO but it is more complicated for heap memory as that is used by the whole application.


Discussion

No Comment Found