Heaps and Stacks
- Heap:The heap is where Java objects are stored. When creating an object using the “new” keyword, the object is stored in the Heap. The heap can exist until there are no more references to the object or until the program terminates.
- Stacks:Stack is where local variables and method parameters are stored. Each time a method is called, a new frame is created on the stack to store the local variables and parameters of that method. When the equation is complete, the frame is removed from the stack and the memory is freed.
Note:
- A frame (also known as an activation record or call stack frame) is a unit of data that is created when a method is called in a Java program. Each frame contains information about local variables, method parameters, and return values.
- Each time a method is called, a new frame is created on the top of the stack, and the method parameters and local variables are allocated on this frame. Values passed into the method are passed into the frame via the frame’s parameter.
- When the method completes, the frame is removed from the top of the stack and the memory is freed. Method return values are passed back through the previous frame in the Stack.
- How to store memory on Heap and Stack differently:
- In the Heap, memory is dynamically allocated by the JVM when objects are created, and memory is released when there are no more references pointing to that object.
- In the Stack, memory is allocated statically when a method is called and the memory is released when the method completes.
For example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Example</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">String</span> global1 <span class="token operator">=</span> <span class="token string">"Hello, world!"</span> <span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token class-name">String</span> global2 <span class="token operator">=</span> <span class="token string">"Hi, world!"</span> <span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span> <span class="token punctuation">(</span> <span class="token class-name">String</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> args <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">String</span> localVar <span class="token operator">=</span> <span class="token string">"Hello, Java!"</span> <span class="token punctuation">;</span> <span class="token class-name">Object</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token class-name">System</span> <span class="token punctuation">.</span> out <span class="token punctuation">.</span> <span class="token function">println</span> <span class="token punctuation">(</span> obj <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
In the example above:
- The global variable
global1
is stored on the Heap, more specifically in the static memory(data segment). When the class is loaded into memory, this global variable will be initialized. - The global variable
global2
is stored on the Heap, but in non-static memory, where all instance variables of a class are stored. It is created when an instance is created. - The main method
args
parameters are stored on the stack because they are method local variables. - The local variable
localVar
is stored on the stack because it is also a method local variable. - The object
obj
is initialized with thenew
keyword, so it will be stored on the heap and a reference to the object will be stored on the stack.
Note:
- When the ” main ” method is called, a new frame is created on the Stack to store the local variables and method parameters including
localVar
and theargs
parameter. Then a new object is created on the Heap using the keyword ” new “, and the address of this object is assigned to the variableobj
. whenSystem.out.println(obj)
the address of the object is printed to the screen.- When the ” main ” method terminates, the frame on the Stack is removed from the Stack and the local variables and method parameters are released from memory. However, the object created on the Heap will continue to exist until it is picked up by the garbage collector.
(At the moment when I write this article, I’m just an ordinary student. So maybe the knowledge I found out above may be wrong or right. In the worst case, it’s wrong, I hope everyone has some understanding. You can help me by commenting below so I can find out again, but I don’t pray for bricks and stones to build a house, so I hope everyone reminds me!
Good luck and thank you everybody!