Introduction
Java applications need a certain amount of RAM on a computer to run. Each time an object or variable is declared, it needs more RAM. Simply designating enough memory to hold every value declared and run each method would lead to a bloated application. The Java Virtual Machine (JVM) divides memory between Java Heap Space and Java Stack Memory in a way that only uses memory that’s needed.
Heap Space
It is created by the Java Virtual Machine when it starts. The memory is used as long as the application is running. Java runtime uses it to allocate memory to objects and Java Runtime Environment (JRE) classes. When an object is created, it is always created in Heap and has global access. That means all objects can be referenced from anywhere in the application.
Garbage collection works to free memory by clearing any by objects without any references in the methods. These are objects that are no longer being used. Clearing them ensures they don’t take up space in the Heap.
Unlike in a Java stack where memory allocation is done when your program is compiled, in a heap it is allocated as your program is run. Accessing variables placed here is a bit slower compared to a stack’s direct and fast access.
void somefunction( ) { /* Create an object "m" of class Member this will be put on the heap since the "new" keyword is used, and we are creating the object inside a function */ StackHeap m = new StackHeap(); }
Stack Memory
This is the temporary memory where variable values are stored when their methods are invoked. After the method is finished, the memory containing those values is cleared to make room for new methods.
When a new method is invoked, a new block of memory will be created in the Stack. This new block will store the temporary values invoked by the method and references to objects stored in the Heap that are being used by the method.
The memory size of a Java stack is generally much less than in a Java heap space because when a method ends, all the variables created on the stack are erased forever.
void somefunction( ) { /* Create an object "m" of class Member this will be put on the stack since the "new" keyword is not used, and we are creating the object inside a function */ StackHeap m; } //the object "m" is destroyed once the function ends
Stack vs Heap
Both are ways that Java allocates memory and both are stored in the RAM. However heap is used for dynamic memory allocation, while stack is for static allocations.
Variables that are allocated on the stack are accessible directly from memory, and as such, these can run very fast. Accessing objects on the heap, on the other hand, takes more time. On the stack, memory allocation happens when the program is compiled. Meanwhile, on the heap, it begins when the program is run.
Allocated memory on Heap is not managed automatically nor is it as tightly managed by the central processing unit the way stack is managed. You would need to free allocated memory yourself when these blocks are no longer needed. The heap is prone to memory leaks, where memory is allocated to unused objects and will not be available to processes other than that.