JAVA Stack vs Heap

https://www.educba.com/java-heap-vs-stack/

Java virtual machine (JVM)which allocates some memory form the operating system, JVM uses this memory for creating objects and instances and this memory is called Java heap. Java heap is used as a dynamic memory allocation. It is mainly located at the bottom of the address and garbage is collected when heap size becomes full. The local variables that will be stored and method invocations are present in the specified memory that is called Stack. Stack memory follows the rule of Last-In-First-Out (LIFO). A stack is referred to as static memory allocation. The size of the memory stack is less as compared to heap memory size.
Let us study much more about Java Heap and Stack in detail:

Java heap is divided into two main parts that are Young space and Old space. The young space is part of Java heap memory which is allocated or secured for storing the new object creation. When this space becomes full and valid for a certain period and now these are not in use then it moved to other parts that are Old space which is reserved for taking the old objects.
In java Heap, the garbage collection is the process to clear the objects which are dead or that are not in use, which helps to free the space from the heap and make space for new objects or instances.
While the method is getting invoked then its stack frame will put on the top of a call stack. The stack frame holds the state of a method which is having the particular lines of code that are getting executed and all the local variables. The current running method of the stack is always the method that is at the top of a stack.
The block has been created in a stack when the method is invoked to hold the values and object reference of the methods. After the execution of a method, the block is not in use anymore and becomes free which can be available for the next method.
The stack is used for executing the threads. Each thread has Java virtual machine stack and JVM stack stores frames. The methods are allocated to stack memory and access to memory is really fast. We cannot alter the Java virtual machine stack, it can be done only through push and pop on the java stack. Java stack becomes larger and reduced as push and pop have done with local variables. JVM plays its part on method invocation and return. In simple terms, Java Stack is to store methods and variables.
在这里插入图片描述
Some of the points are explained below that shows the difference between Java Heap vs Stack

Java Heap is the section of memory in which elements can be stored and removed in any order. In Stack, the elements can be stored and removed by following rules of Last in First out (LIFO).
When Java Heap is fully occupied then it throws out of memory error or Java heap space error. When stack memory is occupied, it throws stack overflow error.
For Java Heap, Xms and Xmx java virtual machine option can be used to define the start size and maximum size. For Java Stack, Xss JVM can be used to define the size of stack memory.
When the new object has been created, it simply gets stored in Java heap memory. The reference of the new object has been stored in stack memory.
Java heap can be used when a user does not have any idea about the amount of data required at runtime. A stack can be used when a user knows exactly the amount of data required before compile time.
In Heap, there is no dependency on any element to access other elements. Any element can be accessed randomly at any time. In Stack, there is particular order to access the element.
Heap is more complex as sometimes it cannot know whether memory gets occupied or free. In a stack, it is simple and easy.

发布了83 篇原创文章 · 获赞 0 · 访问量 850

猜你喜欢

转载自blog.csdn.net/michaelforgood/article/details/103578038