hi welcome back
to techshare channel so in this java interview
questions
we're going to discuss about another um
important the fundamental question asked
to all the java developers
to understand how good knowledge he has
about a java memory management
about the different layers in java
memory
management and what will be how the
garbage collection works in each layers
and how the developers can handle
um the memory related issues and
what is the uh how
uh he considered to allocate a memory
for each
layers right so these are different
things
the panelists want to understand from
the developer
um so let's
get into the discussion
so this is how the memory
stack looks before java 8 so we have a
stack memory and we have a heap memory
the stack memory actually stores the
variables used inside
the method and the heap actually stored
actual
objects and in the heap you can see
uh different segments like young generation
old generations so let's see what
those um layers are right
generation
um let's say like uh i'm creating the
object for the first time
so those will be moved into the hidden
memory
um the call basically can always start
the garbage  collection from the eden memory
and if there is any object surveying uh
from the call   then
those will be moved to the survivor memory
so from which it will be garbage
collected
later on if there is any objects of the
long lifespan
then those will be moved to the old
generation memory so
that is how um the objects are moved
between the young
generation and old generation and we
have a perm generation
so the perm generation actually stores
the metadata of your class
also it stores the string interns and
string interns okay so those information
will be stored in the perm generations
now that is how so this is how
the memory stacks of
the java looks before java 8 but
when the java 8 come in picture they did
some changes
in this memory stack so one of those is
moving the perm generation out of the heap
so um they moved the perm generation to
the meta space
so what is the advantage um they
achieved because of moving the perm
generation out of
the heap is like of the perm generation
actually you know it stores the metadata
of your class right
so let's say like you have met your
application is too big and it depends
upon too much of libraries
so when you start loading
your application you need to load too
much of
metadata so
which may end up in you know um
filling up your perm generation because
um being
part of heap you will be setting what is
the
maximum memory right and
it is um there lot a lot of possibility
that memory may get filled okay
and if once they get filled the garbage 
collection need to start working
on your perm generation to
clear out all those you know memories
right so which takes long time
and because of that because of all these
things
there may be a performance issue
okay so to avoid that performance issue
they moved the perm generation to
out of heap and they named it as a meta
space
so the meta space is created on a native
memory
which can keep growing like like if
let's say like initially you have like 2
mp of meta space
and once you reach the 2 mb it can you
know
keep growing to 4 mp and 4g then it can
keep growing like
that is a major advantage of
moving the perm generation to the meta
space
okay so this way like you were the
performance of
the application is not impacted
and um the other change they did is
they added a keep memory in the
young generation
uh the purpose is um
so they don't want to impact the
first you know objects um
like took to be eligible for a
garbage collection so they want to
keep it for some time to memory
that is the intention of having to keep
memory so whenever
it's created for the first time it
will be
stored in the keep memory for some time
and then it will be moved to either
memory from which
the callbacks connection actually start
working
okay so that is the purpose of having
that ignore
yes so that's all about
know how the stack looks like okay and
so here is you know different ways how
to allocate a memory
um like say like if i want when starting
the application
if i want to set some memory for heap i
can use this
sms parameter and there's smx
and the maximum size of my hip and
now the perm size it or argument which
i can use um
to set the
perm space size and to set a meta space
i can use the
max meta space size okay so
yeah like
um so after moving the pumps
generation out of the keep and throwing
into a native memory
still we need to handle you know uh the
meta space we need to allocate a memory
for the meta space
what will happen if we are not
allocating and
your program may be you know hoping too
much of memory internet is completely
space
and that may end up in other some other
issues so it is better to you know
allocate some memory
okay some maximum memory and better
space
so that is why we are using the max meta
space
uh parameter okay so these are the
different jvm parameters
when you're starting the application you
can pass these parameters or you can
set these
let's say environmental variables okay
so
so these are few different types of
exceptions which may occur because of
the issue okay so
the only stack overflow exception
so we all know like the stack contains
the variable that are declared inside a
method right
so the stack overflow normally happens
if let's say like i'm
using a recursive function and because
the function is trying to call
another function so indeed this because
you
end up in some infinite loop so what is
happening my stack is
workload
this calling and storing the same
method second and again and again that
is why
you you may end up with a stack also
existing then you need to go into your
code and see
why what is the what is um
why that infinite loop is happening so
you need to fix the infinite loop and to
resolve the
stack of exception and
the memory out of memory exception of
them
space exceptions because like um
maybe we are soft the memory leak
it's like you know for example like you
got a data
um you got a connection full object
okay and you forgot to you know release
the connection pool
okay so you are storing that always
in your memory right so every day when
you get a connection
you're creating a new connection whole
object and you
keep storing that in your memory you are
not releasing it
right so that may end up in your memory
that's just one
example there may be many other use
cases
for example like if you use
too many of static variable that also
end up in out of memory exception okay
so
um so these are different exceptions
that may
happen because of some um
um unclean
code okay and how to
how to find okay how your application is
performing in terms of memory right
how much memory your application is
actually eating
right so how to find those things you
can use
you know the jvm itself is providing the
utility right
like um jstat jpmap and in id
like eclipsa networks they also
providing
profilers to find out like
how much memory it is using and where it
is actually consuming more
memory and like which class and which
method
is actually heating up more memory so
all those
profiling you can do using these
utilities
okay so um
from my experience okay so even i have
been working some of
the java based batch jobs okay which
you need to um
process too much of data okay so how
we handle okay from design perspective
we'll analyze like how
much of them how much of data i'm going
to
you know play right so based on that we
allocate a memory
when designing the job itself okay and
whenever there isn't any issue
we get that dumped from
the production support team program like
if there is any issue in the
production okay we get a memory down
from the production support team we
study the memory down
to see where the issue is happening and
based on that
we fix like that particular class or
object
we fixed and that is how in real time we
handle the
memory issues i hope
um it is a useful section for you all
guys
if you like this session uh subscribe to
my channel
so
i'm planning to add more sessions uh on
the same topic
so yeah so please subscribe to my
channel
so you get update about a feature
feature sessions thank you
