Difference between heap and stack
About heap and stack memory overview so
knowing how memory actually works in
Java is important as it gives you the
advantage of writing high-performance
and optimized applications it helps you
to understand the scope of variables
object creation and memory management in
the main in Java there are two main
memory types stack memory and heap
memory as far as stack memory is
concerned it contains method calls local
variables and reference variables on the
other hand heap memory contains objects
and instance variables you may post a
question that what are instance
variables for example when we have a
card loss then we can define private
integer for example speed we can define
private string name of owner and
something like this so these variables
that belongs to the class itself these
are called instance variables and these
variables are stored on the heap memory
of course because these are associated
with the object itself
okay so stack memory contains primitive
values that are specific to a method and
references to objects that are in a heap
referred from this given method the Java
heap space is used by Java Runtime to
allocate memory to objects and Java
runtime environment related classes
whenever we create any object it is
always created in the heap space okay so
let's take a look at a concrete example
every single application starts with the
main method because it is a method call
it's going to be inserted into the stack
memory automatically so in the stack a
frame will be created from the main
method and if we create a given variable
for
e.g: a double with value 10 then
this local variable D in the main method
will also be created in the main methods
frame on the stack memory
why the main method has a
local variable D if we make a given
method call then
this public void method 1 will be cooled
with the given argument so what does it
mean a method calls are stored on the
stack memory so there's going to be
another stack frame for method 1
ok so
if we create the method one specific
local variable a floating-point number
with value 30 then these values are
going to be stored on the stack memory
associated with the given method so
method 1 stack frame is going to contain
the argument I and local variable F so
as you can see every single method has a
distinct frame on the stack memory and
by the way these stack frames are
independent so for example within the
main method we are not able to alter the
value of the local variable F because
they are in different
stack frames anyways if we call for
example an order method method 2 which
is a public void method again it is
going to be stored on the stack as usual
and if we create a new object for
example we have a class house with
instance variables windows and doors if
we instantiate a new object with the new
keyword then this house object is going
to be stored on the heap memory ok so as
you can see a new object is created on
the heap memory with instance variables
so it is crucial that instance variables
it may seems to be very similar to local
variables but they belong to a given
class so local variables are stored on
the stack but instance variables that
belongs to a given class they are stored
on the heap memory and the reference
itself in this case the house ref is
going to be present on the stack memory
ok so as you can see the house reference
is on the stack memory this is what we
have been talking about in a previous
lecture that the reference itself is on
the stack memory about the object it is
referencing is
on the heap okay so the assignment
operator makes the house rap reference
variable to point to the house object in
the heap memory okay so there's the
reference from the house raff present on
the stack to an object present on the
heap memory so what's going to happen
when we finish with method 2 when method
2 execution is completed the flow of the
control will go back to the command
method warm because method 1 called
method 2 you
that's why Java is going to consider
method 1 again okay so this is what's
happening here and what's crucial that
beakers method 2 is completed it is
flushed out from the stack so this
method to relate its stack frame is
going to be removed from the stack so
what does it mean exactly that there is
no active reference to the object
present on the heap so what is it mean
exactly that this object is eligible for
garbage collection so when the job of
your tool machine runs the garbage
collector the object will be destroyed
because there is no active reference to
that given object this is exactly how
garbage collection works after method 1
is finished with its execution then
method 1 stack frame is going to be
removed from the stack memory and the
flow control will go back to the command
main method so this is why this stack
frame is removed from the stack memory
and we go back to the main method and
because there are no more operations in
the main method which means that we are
finished with the execution of the main
method the main method related stack
frame is going to be removed from the
stack as well ok so this is
How stack memory and heap memory works
method calls arguments and local
variables are stored on the stack memory
but objects on the other hand are stored
on the heap memory so whenever we create
a new object then the reference itself
is on the stack memory and the object
itself is on the heap memory so this is
the crucial difference between stack and
heap okay so any object created in the
heap space has
access and can be referenced from
anywhere of the application the stack
memory is always referenced in a leaf
also last in first out order push
operation adds an element at the top of
the stack and the pop operation removes
an element from the top of the stack so
I'm not sure whether you are familiar
with abstract data types and data
structures but stack is the most common
abstract data type we can store items in
a lethal manner so the last item we
insert is the first one we take out in
this case the stack is going to contain
method frames and within the method
frames there are local variables and
argument values okay we can use Java
Virtual Machine options to define the
startup size and the maximum size of the
heap memory and the stack memory so we
can control that what's going to be the
size of the stack memory and the heap
memory what's crucial that when stack
memory is full then Java Runtime shows a
stack overflow error whereas if heap
memory is full it's going to throw an
out of memory error so this is crucial
as a software engineer to be able to
detect whether the stack memory is full
or the heat memory is full and we can
conclude based on the error itself stack
overflow error has something to do with
the stack and out of memory error has
something to do with the heap memory.
Post a Comment