JVM Memory: Stack and Heap

In this article, we are going to discuss 'Stack' and 'Heap' that are part of JVM memory. let's begin.

Whenever a Java application runs, JVM uses some of the storage area of our computer memory (as Java applications run inside JVM). this storage area is used to store data, and as we discussed here Java and Java is OOP(Object Oriented Programming) therefore data is related to objects, classes, and variables.

Now, this memory area is divided into 2 parts stack and heap. lets discuss one by one.

  • Stack :

    • As most of us are already aware that Stack is a data structure, it works on the principle of LIFO (it means that the element that inserts in last comes out first).
    • Here, JVM uses the stack to store local variables. as the stack helps in maintaining the variable's scope efficiently therefore JVM developers choose stack data structure to manage local variables. here scope of a variable means the accessibility we have for that variable. as we know local variable scopes are limited between the start of opening curly braces and the end of closing curly braces.
    • Now using stack, whenever we are reaching a closing bracket, any local variable that we declared inside the block that we are leaving is popped off from the stack and gets destroyed.
    • Also, local variable have short lifetime as compared to object and global varaibles. therefore we can say that stack allow us to store data that have short life time.

  • Heap :

    • Now let's discuss heap. first of all the heap that we are discussing here is not the data structure that we use to read about in data structure courses like min heap and max heap.
    • The heap is just a big chunk of memory and nothing else.
    • Whenever we create an object using "new" operator in our Java code, then it creates object in heap.
      E.g. Object obj = new Object();
    • Heap stores data that have a longer life such as objects. here objects have a longer lifetime as compared to a local variable as they need to be shared across multiple code blocks.
    • Whenever our Java application runs and if we say that our application consumes some amount of memory that is a heap (except some memory for stack).
    • In a Java application there can be n number of stacks but there will be only one heap that exists. also, one more point to note here is each thread has its own stack but all threads access the same heap.
    • To conclude here , data types like int, double, long are store in stack. where as String, Integer, Double , Long, Customer all will be stored in heap.

Lets discuss more about heap. Actually heap is further divided into two parts young generation and old generation.

What this generation word here means? lets cover it. Whenever objects created first time, it then resided in the part of heap that is called as young generation.
Now whenever this young genration area becomes full then Java garbage collectors comes and sweep objects that are not of any use and the remaining/survivor objects is passed into another part of heap that is called as old generation.
Here one point to note that young generation area is small in size as compared to old generation area and thats why object lives in young generation have short liftime as compared to objects reside in old generation area.

Young generation area is further divided into 3 parts.

  • Eden : its main area of young generation, here first time object creation takes place. whenever it becomes full then JVM invokes GC and unused object then sweep by GC and survivor objects then passed to Survivor area (S0) before moving to old generation.
  • S0(Survivor 0)
  • S1(Survivor 1)
  • Now, Whenever Eden becomes full and first time it moved the object to S0. next time whenever Eden becomes full it moved the object to S1. and these ping pong of objects between S0 and S1 happens for some iterations after that it passed to the old generation.

Apart from young generation and old generation , we have another area in heap that is called as Permanent Generation. but it was there in heap till Java 7.

Till Java 6 Permanent Generation used to store classes metadata and internalized String (String pool).
In Java 7, String pool removed from Permanent generation and String pool become part of old generation in heap. In Java 7, Permanent generation used to store only classes metadata.
from Java 8 and onwarda Permanent Generation has been removed from heap and now it resides in computer native memory. Also, its name has been changed and it is called as Meta Space. Also, meta space is used to store classes metadata.

Sometimes we faced "java.lang.OutOfMemoryError" such type of error while running Java application. these errors can be resolved by tuning heap size.
Below are the options we can use to change heap size.

  • -Xmx : it sets the maximum heap size.
    E.g. java -Xmx20m [Class Name]
  • -Xms : it sets the starting heap size.
    E.g. java -Xms5m [Class Name]
  • -Xmn : it sets the size of young generation.
    E.g. java -Xmn15m [Class Name]
  • -XX:MaxPermSize : it sets the size of Permanent generation. it works only till Java 7.
    E.g. java -XX:MaxPermSize=152m [Class Name]
  • Here, k stand for KB, m stands for MB and g stand for GB.
Below is an example:





Note : Please comment below for any improvement , suggestions, regarding any wrong information present in this article. We will improve it so that only right information available in this article.




Comments