Friday, 29 July 2016

Explain different areas of Java Heap

Heap is a portion of memory where dynamic allocation and de allocations happen. Heap memory is slightly slower to read from and written to, because pointer access is needed to access heap memory. JVM allocates Java heap memory from the OS and then manages the heap for the Java application. Heap doesn't have any restrictions on memory size, it totally depends on Hardware.

Before explain the basic areas of heap, let me answer some basic questions.

In which area Java objects reside?
Java objects reside in heap.

When the heap memory creates?
Heap memory created when the JVM starts up.

Is Heap memory fixed in size?
No, heap memory size may increase or decrease in size while the application runs.

Basically Heap is divided into two areas.
a.   Old space
b.   Young space (Nursery)

Young space
Newly created objects are stored in young space. Whenever the young space becomes full, Java garbage collector runs, remove all the unreferenced objects. In addition to this, objects that have lived long enough in the nursery are moved to the old space, thus freeing up the nursery for more object allocation.

Old space
Old space contains objects that live long enough. When the old space becomes full, a process called an old collection collects garbage.

Thread Local area (TLA)
If the object size is small, it is usually create in Thread local area. Thread Local Area is a memory reserved from heap and given to a Java thread for exclusive use.

What happen if TLA becomes full?
Thread request for new TLA.

In following posts, I explained about different garbage collection techniques.

If you want to know, how to increase heap memory, go through the post ‘How to increase Heap size in java’.

You may like



Provide for-each behavior to custom collection


‘foreach’ statement is used to traverse a collection. Following example explains how to traverse a collection using for-each loop.
import java.util.Arrays;
import java.util.List;

public class Test {
 public static void main(String args[]) {
  List<String> list = Arrays.asList("Hi", "ptr", "How", "are", "you");

  for (String s : list) {
   System.out.println(s);
  }
 }
}


Output
Hi
ptr
How
are
you


To support for-each loop to your custom collection, it should implement Iterable interface. In ‘Implement custom iterator in Java’ section, I explained about Iterable interface. . Suppose my collection has elements like 1, 2, 3, 4, 5, 6, 7, 8, 9. My for-each should traverse the elements at even positions like 1(is at 0th position), 3(is at 2nd position), 5, 7, 9.
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

public class EvenList<T> implements Iterable<T> {
 private List<T> list;

 EvenList(List<T> list) {
  this.list = list;
 }

 public Iterator<T> iterator() {
  return new EvenIterator<T>();
 }

 @SuppressWarnings("hiding")
 private class EvenIterator<T> implements Iterator<T> {
  int size = list.size();
  int currentPointer = 0;

  public boolean hasNext() {
   return (currentPointer < size);
  }

  public T next() {
   if (!hasNext()) {
    throw new NoSuchElementException();
   }

   @SuppressWarnings("unchecked")
   T val = (T) list.get(currentPointer);
   currentPointer += 2;

   return val;
  }

 }

}


Total logic is in the next() method, here I am checking for an element in the collection using hasNext() method, it it returns false, I am throwing NoSuchElementException(). If element exists, then I am saving current element to a temporary variable and incrementing the currentPointer to 2.
import java.util.Arrays;
import java.util.List;

public class TestEvenList {
 public static void main(String args[]) {
  List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

  EvenList<Integer> myList = new EvenList(list);

  for(int i: myList){
   System.out.println(i);
  }
 }
}


Run above application, you will get following output.

1
3
5
7
9


You may like