Tag Archives: heap

Garbage Collection in Java

Unlike C++, Java automatically allocates and deallocates memory so the applications don’t have to do it by its own. But, the way how garbage collection works depends on JVM implementation. In this article I will talk about HotSpot JVM, since it is commonly used in production.

In HotSpot JVM the heap is separated into the generations basing on weak generational hypothesis because empirical analysis of applications has shown that most objects have short life-cycle and there are few old-to-young references. Therefore, the heap is separated into young generation and old generation. This segregation helps to keep a big bunch of dead objects in one place that can be collected very quickly.  At the same time the young generation separated into three spaces eden, survivor 0 and survivor 1.

jvm_heap2

The perm space is the method area that contains information on methods, classes, method’s code etc. One of the biggest improvements in Java 8 is that perm space is removed from the heap. Since in earlier versions if your application loads a lot of classes then you had to tune and optimize this part of the heap. Another interesting improvement is string deduplication it searches for strings which occured more than once and replaces them to pointer that references to a string.

Continue reading Garbage Collection in Java

Binary heap

Binary heap is a tree-like data structure that generally can be separated into two types: min-heap and max-heap. The difference is that in the max-heap all nodes are greater than or equal to each of it’s children. In the min-heap everything is vise versa, all nodes are less than or equal to each of its children. Thus, the highest (max-heap) or lowest (min-heap) element is always on the top and this is the main advantage which provides possibility to access the min or the max element in constant time. Another important property is that all levels, except the last last one, must be full.

heap

The last level can be either full or not, but all new elements always added to the last level, from the left to the right.
Continue reading Binary heap