0:02 hey guys welcome to concept and coding
0:04 and this is shreyansh so today we are
0:06 going to very very important topic in
0:09 Java Java memory management right and
0:11 before we start so I want to say that
0:13 please excuse my voice I have a little
0:16 bit cold so I'm sorry for that so here
0:19 if you see that
0:21 till now we have covered a Basics like
0:24 how to create a class how to create a
0:26 primitive data types how to create an
0:30 object right how to call a method so
0:31 this thing should comes to your mind
0:34 right hey shreyansh how do we manage how
0:36 do we the memories are allocated to them
0:39 right so let's understand this today
0:41 because this is very very important
0:43 interview questions and plus when you
0:45 start working in the company you will
0:47 see that how much importance is given to this
0:48 this
0:51 okay so now let's start this
0:54 so there are two types of memory when
0:56 I'm saying memory it's I'm talking about Ram
0:57 Ram
1:00 right so there are two types of memory which
1:02 which
1:06 Java creates stack and Heap
1:08 and when we are talking about the memory
1:11 management who manage this memory jvm
1:14 jvm is the one which managed this memory
1:17 right and it creates it divides the
1:20 memory into two part stack and he and it
1:22 stores different type of data inside it
1:25 so let's see first what kind of data
1:27 stored in a stack and what kind of data
1:29 stored in Heap
1:33 so let's say this this is your stack
1:38 now here if you see that I am creating
1:41 multiple copies of it I'll tell you why
1:44 and let's say this is your HEAP so
1:46 generally Heap has more memory than a step
1:48 step
1:51 okay and it's only have one copy
1:54 now what kind of data is stored inside a stack
1:55 stack
1:57 so first of all it is stored temporary
1:59 variables right so let's say you have
2:02 created a method inside a block and you
2:05 created a variable in t equals to 10. so
2:08 its life is very temporary with the
2:11 within this Open Bracket and closing
2:14 bracket after that it got destroyed
2:16 so this is known as temporary variables
2:19 right so all temporary variables are
2:25 okay then
2:26 then
2:29 separate memory block for method so
2:31 whenever a method has been created so
2:33 let's say this is inside of one
2:34 particular method public
2:35 public
2:38 get something
2:40 so for each method there is a block
2:45 memory frame you can say that stores
2:47 and inside that
2:50 method whatever the variables whatever
2:52 the difference it's created it will
2:54 store inside this memory frame
2:56 okay so we will I will tell you an
2:59 example with this one but first let's go
3:01 through this what kind of data is stored
3:02 and we will see the example it will
3:06 clarify you more okay but let's see this
3:08 store primitive data type so this is
3:09 also nothing but a primitive data type
3:11 so primitive data type is stored within
3:12 a stack
3:15 store reference of the Heap object
3:18 whenever you create an object like new
3:20 with the new keyword whenever you create
3:23 an object it is stored inside a heap
3:27 its memory but its reference reference
3:29 reference of
3:31 of object
3:33 object
3:40 so this type of reference I will tell
3:42 you later but first understand what kind
3:43 of data is stored so but there are
3:45 different type of reference a strong
3:46 reference weak reference soft reference
3:50 pantham references right so I will tell
3:51 you uh later but first understand what
3:53 kind of data we stored
3:57 each thread has its own stack memory so
3:59 now you know that is why I have created
4:01 multiple copies of Stack because is
4:03 thread so this is let's say thread one
4:06 has its own stack memory this is thread
4:08 two has its own stack memory thread
4:10 three has own stack memory third phone
4:12 has its own
4:14 but if you see that all the threads
4:17 Shear is common Heap memory okay
4:19 okay
4:22 variables within a scope is only visible
4:24 and as soon as variables goes out of the
4:26 scope it gets deleted from the stack in
4:28 the lifo order so what I mean to say is
4:32 that everything is here in the stack is
4:35 within a scope everything has a scope I
4:37 will tell you how so just waited for it
4:40 for an example you will get to know how
4:42 this Leaf order is performed and what is
4:44 mean by the scope
4:47 when the stack memories goes full you
4:50 will get this error stack Overflow error
4:53 okay now let's understand this with the
4:56 example right this is very simple class
4:57 if you see
5:00 I have created a class memory management
5:03 and I have one public static void main
5:04 so it's one method
5:07 okay so this method has one primitive
5:08 data type
5:09 one object
5:12 one string literal
5:15 right one object I created to invoke this
5:16 this
5:19 another method and I'm doing an invoking
5:21 of this method
5:23 and then you can say that this has
5:25 invoked this
5:29 and inside this I have another reference
5:30 to an object
5:32 another string literal and another
5:34 string object this is not a string
5:36 literal so this is string object using
5:37 new keyword
5:40 so it's a very very simple class we have
5:43 covered methods variables right so you
5:45 should be easily understanding this what
5:46 I have written
5:48 but now let's understand this how memory
5:52 is stored for this one
6:10 Heap memory Heap memory is always larger
6:11 than a stack and
6:13 so here let's see this
6:16 first of all main method is invoked
6:18 whenever the main method is invoke I
6:20 told you for each method a block of
6:22 memory is
6:25 stored so let's say I have stored this
6:28 block memory this is for
6:30 for Main
6:36 so this is one scope for the main
6:39 this Frame you can call this as one
6:41 frame of memory
6:44 for which method mean and all the
6:47 variables which are created inside this
6:49 main method it would be stored in its block
6:50 block okay
6:51 okay
6:58 so now let's say main method is called
7:00 we have created its memory
7:02 memory
7:04 now I have created a
7:07 primitive variable primitive variable
7:14 primitive variable
7:16 variable
7:18 10. so as you know that primitive
7:19 variable does not hold any reference it
7:22 stored the value itself so it is stored
7:23 into the
7:27 this memory of this scope
7:29 the second is
7:32 I am creating a new object new person so
7:41 this is your object
7:44 person object
7:46 and this percent object has a reference
7:50 who is storing the difference person obj
7:54 so it has one
7:58 person obj so this holds a reference to
8:00 this memory okay
8:02 okay
8:05 now I am creating one string literal so
8:07 as you already know from the previous
8:09 variable class that string literals
8:12 stores the data into the string pool and
8:14 where does the string pull stores string
8:17 pool is saved inside
8:19 the Heap only so there is something called
8:21 called
8:22 string pool
8:26 so whenever you are creating a literal
8:27 string literal so it would be stored
8:31 here 24 okay and what is the difference
8:34 string literal so it will create a
8:37 reference variable string
8:39 string literal
8:40 literal
8:43 and it is storing a reference to this 24.
8:46 24.
8:49 okay now the third line is new memory
8:51 management so see I am creating one more
8:53 object now so let's create one more
8:56 object here
8:58 I have created one more object what is
8:59 the object
9:02 memory management memory
9:03 memory
9:07 management object and its references
9:11 inside mem obj in the same main one
9:18 So currently if you see that all are
9:21 within a scope so scope is still going
9:23 on scope is not yet finished
9:26 okay so within a scope you can access
9:28 all the variables
9:30 right within a scope so this is a scope
9:33 within a scope you have access to all
9:40 so now ma'am obj is storing a reference
9:42 to memory or memory management object
9:46 now I am invoking a method memory
9:49 management test and passing this
9:52 reference so now you see that I am
9:55 creating an another method so for each
9:57 method it will create a new reference
10:00 so I am creating an another frame
10:09 which method memory management test method
10:10 method
10:12 so this scope
10:14 is for
10:19 memory management test method
10:22 this is kovis for Main and now this is for
10:24 for
10:27 this one so this is its oh
10:30 this method frame memory frame
10:32 so first of all
10:39 it has this
10:41 person obj
10:44 and what reference it has
10:47 so we pass the reference of this one new
10:50 person so new person is here so this is
10:57 okay now
10:58 now
11:03 see I have created uh another reference
11:05 which is pointing to Which object
11:08 whatever the person object is pointing
11:12 to so I have created another reference
11:15 person obj2
11:18 person obj 2
11:21 and it is pointing to the same memory
11:23 which person obj was pointing person obj
11:25 was pointing to this so it was also
11:30 same okay
11:33 okay now
11:34 now
11:36 I have created one more string literal
11:40 24 so whenever I as you know from the
11:42 previous class whenever you created a
11:43 string literal first it will check into
11:46 the string pool so is this literal
11:48 present 24 is present if yes it will not
11:51 create new it will use the same so it
11:53 will create
11:58 string literal 2 reference string
12:02 literal 2 and it point to the same from
12:09 then its create a string literal 3 and
12:11 it's a not a little it's a new object
12:14 because I'm using a new keyword so it
12:18 create a new object which is string object
12:19 object
12:23 and has the value 24 but it is inside a
12:26 new this one a string
12:29 literal 3 and it's storing this
12:31 reference here
12:33 now if you see that
12:36 I have got a closing bracket whenever
12:39 you get a closing bracket means scope is
12:42 finished whenever the scope is finished
12:48 you have to delete all the variables or
12:50 references created within that scope so now
12:52 now
12:54 this scope is finished for the memory
12:56 management test when the memory
12:59 management scope is finished it will
13:01 delete this scope
13:04 it will delete this scope right so now
13:06 if you see that it's going in the le4
13:09 manner last and first out so this one
13:12 was the added last after main but it is
13:14 the first one to get deleted when this
13:17 scope is get deleted so all this will
13:19 also get removed so now
13:21 this is removed
13:23 so this reference
13:29 after that
13:31 this get removed
13:39 okay now after that
13:41 this will get removed inside this this
13:44 will get removed right so this reference
13:49 after that
13:52 this will get also removed
13:56 so its reference will also be removed
13:58 so now you see that
13:59 it has done
14:01 remove this
14:04 memory management test method frame and
14:06 all the variables we have created we
14:08 have removed in a lifo manner we have
14:10 cleaned up okay
14:11 okay
14:14 now it moved to here now the control
14:16 come backs to here this method is
14:18 completed now main method closing
14:21 bracket has been reached now main method
14:23 closing bracket has been reached means
14:25 main method scope is also finished so we
14:27 are removing
14:31 this also when we are removing this so
14:32 what we are doing is we have to remove
14:34 this memo BJ
14:36 when memo widget is removed we have to
14:38 delete this reference so this reference
14:40 is removed
14:45 then we remove string literal and this
14:48 reference will be deleted
14:52 okay then person obj reference is deleted
14:53 deleted
14:57 its reference will also be deleted then
14:59 then
15:02 this value will be deleted so now if you
15:04 see that the method execution is completed
15:05 completed
15:07 right stack is empty
15:10 and everything is done but now you have
15:13 noticed one thing here
15:15 what we have deleted the references
15:18 which was pointing but what about this
15:21 objects which now created in the Heap
15:24 how we will delete those because they
15:25 are reference when their references was
15:28 deleted so whatever was pointing that we
15:30 have deleted that reference
15:33 but now these objects are left in the
15:35 Heap who will clean this up
15:38 that's where the garbage collector comes
15:40 into the picture garbage collector is
15:43 used to delete the unreferenced object
15:45 from the Heap
15:48 so now for this object is there any reference
15:49 reference
15:52 from the stack no there is no reference
15:54 so garbage collector will remove this
15:56 now it will check hey is there any
16:00 reference is anybody referencing no
16:02 nobody is referencing this will also be
16:03 freed up
16:05 hey for person object is there anybody
16:07 referencing no nobody is referencing
16:09 this will also be freedom
16:11 okay similarly for the string pull if it
16:13 is not used afterward it would be removed
16:14 removed
16:17 right so this all will be done by
16:19 garbage collector garbage collector runs
16:23 periodically right jvm controls jvm
16:25 controls when to run the garbage
16:27 collector so see
16:35 so in your application code if you write
16:37 this command you are telling that hey
16:41 run the garbage collector and garbage
16:43 collector will try to scan the Heap hey
16:46 what all object there is no reference if
16:48 you find any object for which there is
16:51 no reference it will delete it but jvm
16:52 says that
16:55 there is no guarantee that even if you
16:57 type this I will run the GC so running
16:59 the garbage collector is totally depend
17:00 upon jvm
17:03 so jvm has full control over garbage
17:05 collector whether I should run it or
17:08 when to run it okay so even though you
17:09 write it there is no guarantee that
17:10 garbage collector will run and that's
17:13 why it is called as automatic
17:15 memory management
17:18 right so you don't have to worry about
17:21 freeing up the memory jvm takes care of
17:24 it automatically it periodically it
17:26 keeps on checking the Heap memory and if
17:28 it seems that okay Heap memory is
17:30 filling up fast so it will run the
17:32 garbage collector more periodically more
17:34 faster if it see that okay we have I
17:36 have sufficient space no need to worry
17:40 jvm may run a garbage collector very less
17:41 less
17:44 okay so you understand that
17:47 so now you have understood this
17:50 stack memory how it stores the block how
17:52 it store the variable how it is stored
17:55 the primitive data type how it is stored
17:57 the references now I'll tell you about this
17:57 this
18:00 strong reference week reference
18:02 okay and also you have already know
18:05 about how each threaded's own stack memory
18:06 memory
18:08 variables within a scope is only visible
18:10 and as soon as the variable goes out of
18:12 the scope it gets deleted in leaf order
18:15 I think you get it by the scope right
18:19 so this variable is not visible inside
18:20 this scope
18:23 right so each variable is visible within
18:29 and when the stack memory goes full it
18:32 shows stack Overflow error okay so now
18:34 let's understand this what are the
18:41 so now in a simple way I have told you
18:43 this way that
18:45 for a stack
18:51 this is a stack
18:53 this is he
18:56 AP stores the object
18:58 this stores the reference
19:01 of object
19:08 so now let's say this example I have
19:10 created here person
19:17 person obj equals to new person
19:24 okay so I've created an object of person
19:27 and P object is the reference to it so
19:30 this is p o b j it's a reference to this
19:35 this is known as strong reference
19:37 right so this is known as the strong
19:39 reference like how we are using it as of
19:42 today mostly this is known as a strong
19:44 reference so when garbage collector
19:46 invokes now garbage collector runs now
19:49 we see that hey is there anyone who is
19:52 referencing to this object it will see
19:54 that yes there is a strong reference so
19:57 it will not delete this it will survive okay
19:59 okay
20:07 weak reference how you create a weak
20:12 reference there is a class called weak reference
20:14 reference
20:16 you want to create a weak reference of person
20:20 now you are saying that okay this is weak
20:22 weak
20:23 p o b j
20:33 of person
20:35 and you are creating a new object new
20:42 okay now see this
20:44 whenever you create an object like this
20:47 so you are creating a new person object
20:48 let's say you are creating another new
20:51 person object
20:55 okay so this is another P person object
20:58 but you are making a weak reference
21:00 right you are creating it a weak
21:03 reference so now here you are creating a
21:08 weak
21:13 p o b j so there is a reference
21:15 but it's a week
21:18 what does it weak means weak means that
21:22 as soon as the garbage collector runs as
21:25 soon as garbage collector get invoked it
21:27 will freed up this space
21:31 right so it's not like a strong strong
21:33 says that hate garbage collector don't
21:36 delete my object because I have a strong
21:39 reference somebody is using it
21:41 but weak reference is that till garbage
21:44 collector is not run I am able to access
21:47 it but let's say garbage collector runs
21:49 it is not mandating the garbage
21:52 character that you cannot freed up a if
21:55 you run free it up right and if we try
21:57 to access what object what value it will
21:59 get null
22:01 okay so that's the difference between
22:04 strong and weak strong tells the garbage
22:07 collector that do not delete this I have
22:08 a reference
22:12 but week says that I have a reference I
22:15 can access the value but if you runs you
22:17 can feed it up right as soon as you turn
22:19 you can feed him you can make it free
22:22 and if I try to access it after that I
22:25 might get null value so that's weak
22:27 and there is something called Soft reference
22:29 reference
22:31 soft reference is a type of weak
22:34 reference right what it tells is hey
22:37 garbage collector you are allowed to
22:39 feed it up even though I have a soft
22:42 reference but only do it when it is very
22:45 very very urgent very very urgent means
22:48 that there is no more space and you have
22:51 to create a space then go ahead you can
22:53 remove it no problem okay
22:54 okay
22:57 so only difference between a weak and
22:59 soft is like weak is like as soon as
23:00 garbage collector will run it will feed this
23:01 this
23:04 it will remove this object
23:06 soft reference tells that that yes
23:08 garbage collector you are allowed to
23:10 free this
23:13 but do it if it is very very urgent and
23:16 you need the space very urgent okay so
23:23 okay so generally we hardly use this in
23:26 the company we are mostly using a strong
23:29 reference part okay but it's good to
23:30 know this we have this type of
23:32 references also weak reference soft
23:34 reference a strong reference if there is
23:36 a strong reference present to an object
23:37 this object will never get deleted till
23:39 a strong reference is present
23:42 a weak reference is present as soon as
23:43 garbage collector will run it will
23:45 remove this no matter weak reference is
23:47 present or not and if we try to access
23:50 it it will get null value
23:52 if it is a soft reference GC might
23:54 decide that okay I have sufficient space
23:57 let's keep it alive so it's upon GC
23:59 totally it is allowed to remove it if it
24:01 want but GC will say that okay I have
24:03 sufficient display let it live for some
24:06 more time right so soft is just this
24:08 but totally GC is allowed to feed it up
24:11 but only do if there is a very shortage
24:13 of space okay
24:15 okay
24:17 understood this one references different
24:24 now before we jump into the Heap very
24:26 very important I just want to tell you
24:30 that how the reference can be changed so
24:37 this is a stack
24:39 let's say I created this one
24:42 a person
24:47 obj equals to Nu
24:49 person so what will happen in this case
24:52 a person object will be created
24:54 obj reference will be there in the stack
24:56 it will pointing to this
25:02 so first if I do this obj equals to null
25:04 means what I am doing is
25:09 I am making this removing this reference
25:13 and storing obj null value
25:16 so now garbage collector can invoke and
25:20 okay
25:22 another way is let's say I so this is
25:26 one object and currently
25:33 okay so currently it is pointing to this one
25:35 one
25:37 now I am getting another object person
25:42 obj 2 equals to new person
25:44 person
25:46 okay so what I'm doing is I'm getting
25:49 another object so this is uh percent
25:51 object one
25:54 this is person object two I am creating
25:57 a new object and obj 2 is the one so
26:00 this is obj one let's say this is obj2
26:02 this is storing reference to this one
26:04 if I do this
26:08 obj1 or this is let's say one obj one
26:10 equals to obj2
26:13 so means what I am doing is obj1 is now
26:15 storing the reference of
26:17 this one one
26:21 obj1 equals to obj2
26:22 so this one
26:24 so now here if you see that garbage
26:26 Collective and garbage collector invokes
26:27 it will freed up this because there is
26:29 no reference to this one
26:32 so there are different ways like garbage
26:36 collector can get info right so there is
26:39 no reference to this one it get invokes
26:41 and you can manage the references
26:43 through which and if Heap is getting
26:44 full also then garbage collector got
26:46 also involved okay
26:48 okay
26:51 so whenever object is not present no
26:55 reference is present it is allowed to
26:57 freed up the memory by the garbage collector
26:58 collector
27:02 okay so now we know how the what kind of
27:04 data stack store what kind of data Heap
27:07 stores now get little bit more into the
27:09 Heap memory right
27:14 now let's go into the Heap memory
27:17 I hope you are finding this topic very
27:24 so Heap memory is divided into two parts
27:37 and there is one more part which is
27:49 okay in the previous version of java I
27:51 think before Java 7 it is called Prime
27:54 Jam Prime Jam but now it is no more use
27:58 it is use the meta space it is called as
27:59 meta space
28:02 okay so this is not a part of Heap
28:04 memory so Heap is generally so this is
28:06 your HEAP memory
28:08 which is divided into two part Young
28:10 Generation and old generation
28:13 So Young Generation is further divided
28:20 Eden s0
28:22 s0 S1
28:23 S1
28:27 this is known as survivors Survivor
28:28 Survivor
28:30 Survivor space
28:33 and this is Eden okay
28:35 now let's understand this very very
28:38 simple right but very very interesting
28:43 how when you create an object
28:46 where it restores first
28:54 Young Generation
29:01 this is your Eden
29:03 Eden
29:07 this is your s0 Survivor one
29:11 Survivor sir s0 S1
29:15 okay now I am creating an object one
29:18 I am creating a new object one so first
29:20 of all whenever you created a new object
29:23 it goes inside Edon okay
29:29 this is your object one I am writing o1
29:33 to save a space
29:36 so it is object one
29:37 let's say I created another object
29:42 object two object 3 object 4 object five
29:44 so I have created Five object So
29:46 currently everything was empty so I have
29:47 created Five object
29:49 object to
29:51 object 3
29:52 object 4
29:55 object 5. so five objects are created
29:57 inside a Eden now
29:59 now
30:01 let's say after a periodic time after
30:03 some time garbage collector got invoked
30:08 so first time garbage collector invokes
30:10 now garbage collector do two things so
30:12 there is a something called an algorithm
30:15 called Mark and sweep algorithm
30:18 Narcan sweep I'll go so what it will do
30:20 is as a part of First Step mark it will
30:25 Mark all the objects which is no more
30:29 reference and it is allowed to delete so
30:32 let's say object 2 and object 5 is
30:35 something there is no reference there is
30:37 no reference like this and it is allowed
30:39 to be deleted there is no reference
30:41 let's say object to so it's market like okay
30:42 okay
30:44 object to
30:47 and object 5
30:49 are allowed to be deleted there is no
30:51 reference then there is something called
30:54 sweep algo now what this sweep algo will
30:56 do is it do two things
30:58 it will first remove this
31:01 it remove this
31:03 from the memory
31:05 and what it will do is it will sweep
31:09 this other the surviving objects into
31:13 one of this Survivor extract uh you can
31:21 this o1
31:23 o1 O3
31:26 O3
31:31 o4 will come into s0 okay and it will be
31:33 removed from here
31:37 and this 2 will get deleted
31:40 and it will age will be added so age is
31:46 its one its age is one its age is one
31:48 age is one so means their age is increasing
31:49 increasing
31:52 right so this process is called minor GC
31:55 minor GC minor garbage collector because
31:57 it's very fast it happen very periodically
31:59 periodically
32:02 now let's say second time now let's say
32:04 uh another objects created before any
32:06 garbage collector runs another object
32:10 created let's say obj6 obj7 so where
32:12 does obj six and obj7 will be created
32:15 let's say here obj6 obj7
32:17 obj7 okay
32:18 okay
32:20 because every time a new object is
32:22 created it is created inside the Eden only
32:23 only
32:25 now let's say
32:28 garbage collector got invoked after this
32:31 so first it will do Mark so now it will
32:33 do a mark into all Eden h0 S1 like
32:36 whatever the objects hey which one there
32:38 is no reference so let's say
32:41 obj7 there is no reference so it's
32:42 marking it
32:44 obj4 there is no reference it is marking
32:47 it now they are eligible for deleting
32:50 now sweep will happen so what sweep will
32:53 do is it will delete this
32:56 remove from the memory and
32:57 and
32:59 what it will do is that
33:01 in the other
33:03 so alternatively first it put into s09
33:07 now it will put it to S1 so obj6
33:13 so object 6 is put here now it's ages 1.
33:17 object 1 is put here its age was one now
33:20 it's got age increase to 2 ob3
33:22 ob3
33:25 its age was one now its ages
33:28 2 again Plus 1.
33:32 and this will get removed obj object 6
33:34 is already there with age one so this is
33:36 you don't get freedom
33:38 this got removed
33:40 and this got freedom so if you see that
33:43 at a point
33:45 Eden would be free after the garbage
33:48 collector and one of the s0 and S1 would
33:50 be free okay and alternatively we put
33:52 the data into s0 as one and we increase
33:54 the range
33:58 now let's say I have set my threshold
34:00 threshold value has like age three
34:04 whenever H3 is reached I have to promote them
34:06 them
34:08 I will tell you what does it mean so now
34:11 let's see I have created one more object
34:13 let's say object eight so in which
34:16 memory object will be created newly here
34:17 here
34:19 object 8.
34:21 okay I have created let's say object nine
34:22 nine
34:26 now third time garbage collector runs
34:28 now garbage collector runs what it will do
34:29 do
34:35 Mark the objects which there is no
34:38 reference so now let's say it's marking
34:41 that okay I have to delete this one
34:44 I have to delete this one
34:46 one
34:48 okay now the sweep will happen and what
34:50 sweep will do is it will delete this
34:53 it will delete this feed of the space
34:55 and what it will do is
34:57 it will put
35:01 object 9 from Eden to this age one
35:04 and from S2 it will bring
35:08 object 6 Edge is one here then it will two
35:09 two
35:11 object one
35:15 age was two now age will become 3.
35:18 now here if you see that one thing
35:21 so let's first clean up this object nine
35:23 is there object six has been moved to s0
35:26 this freedom
35:31 and object one has been moved to s0
35:37 now here if you see that I have defined
35:39 a threshold age of three and after three
35:41 I have to do promotion so what does it
35:45 so now here if you see that object 1 has
35:47 reached its age it has survived this
35:50 much time so whenever it survive a
35:52 particular threshold it has to do a
35:53 promotion and what promotion means is
35:56 this object has will be now moved to Old
35:58 generation this object one
36:07 Okay so now in the old generation only
36:08 difference is that
36:11 here it is called minor GC here it is
36:13 called major GC whenever GC does why
36:16 because it first of all whenever the GC
36:18 doesn't say here it don't run too periodically
36:20 periodically
36:22 right maybe let's say here it is run
36:24 every one time in 10 second let's say
36:27 I'm just saying it right but let's say
36:28 in Old generation it might be running a
36:30 one time only in half an hour I am just
36:33 saying that theoretically that here the
36:37 GC runs very less periodically very very
36:41 less as compared to the young and also
36:43 whatever the objects are present into
36:45 Old generation there is kind of a big
36:48 objects right they are used too frequently
36:49 frequently
36:53 right so it takes a lot of time uh so
36:56 these are like faster as your Young
36:58 Generation right so you can say that
37:00 objects created deleted created deleted
37:02 so it's very fast but old generation
37:04 generally happen is like there are a lot
37:06 of references because they are alive
37:08 from so long they are alive
37:10 from so long so there could be so many
37:12 references pointing to them and all
37:15 right and they might also have a further
37:17 references to them so generally this
37:20 process is uh garbage collection is
37:22 little bit time taking then
37:25 Young Generation but it's uh it has it
37:28 do the same thing it first do Mark if
37:31 there is no reference and then delete it
37:33 but here the garbage collection process
37:35 is called major GC here it was called
37:38 minor GC
37:40 you understood that
37:43 young how Heap memory is divided into
37:46 Young Generation and old generation and
37:49 generation we have Eden and Survivor space
37:49 space
37:52 now there is something called meta space
37:55 right so this is a outside of the heave
37:58 and what data it is stored your class variables
38:00 variables
38:04 I think you have seen the variable uh
38:06 topic right you have know what is class
38:09 variables every object which you create
38:12 through static that is class variables
38:14 so it is stored class variables it
38:21 store information about the class from
38:23 which objects can be created right it
38:26 create store constants
38:29 right uh you can Define right static
38:32 final so these are constant so all this
38:34 kind of data metadata information
38:37 constant class variables
38:40 are stored into the meta space right so
38:43 as soon as like Java jvm need need to
38:45 load certain class it will load it here
38:48 as soon as jvm doesn't need any class so
38:51 it will remove it from here right
38:59 now we have understood about the Heap right
39:00 right
39:04 Young Eden Survivor old old generation
39:06 is also known as tanion
39:10 permanent generation so that is uh PRM
39:13 jam it is used to be earlier one now we
39:15 call this meta space
39:17 I have already told you right so this
39:20 was in the old
39:23 old Java versions
39:28 I think before uh before Java 7 I think
39:29 you can check it once I think it is
39:32 before Java 7 it is a prime jam and the
39:34 major difference between prime jam and
39:37 meta space meta space I think from uh
39:40 from java 7 or Java 8 we are using metaspace
39:41 metaspace
39:43 right but here the major difference
39:46 between them is like uh
39:53 was a part of so you have young you have
39:56 old then you have Prime gem so it's a
39:58 part of a heap memory and it is not
40:00 Expendable so as soon as it will get
40:02 filled up
40:04 you will get out of memory error
40:07 but here meta space is
40:10 out of this Heap is now only young and
40:12 old and meta space is separate that's
40:15 why it is called as non-heap and it is
40:18 expanded Expendable as you required okay
40:19 okay
40:21 so that's the major difference but the
40:27 now let's understand about the garbage
40:30 collector algorithm which is we are kind
40:32 of uh
40:38 garbage collector so I think you already
40:40 know by here what is garbage collector
40:43 or how it get invoked like what is the
40:45 purpose of using the garbage collector
40:46 and one of the algorithm I have already
40:54 so as a part of first phase mark it will
40:56 identify water objects need to be
40:58 deleted and the sweep it will just
41:01 remove those objects from the memory
41:04 right so there is something called Mark
41:07 and sweep
41:11 with compaction compact memory
41:13 memory
41:14 so what is the difference here is simple
41:18 nothing much so here if you see that
41:23 uh let's say this space memory space Heap
41:25 Heap
41:27 so here you have one object which is
41:30 identified that it can be deleted there
41:33 are objects here let's say
41:35 and during the Mark I have identified
41:37 what all object need to be deleted let's
41:40 say I have identified this identify this
41:43 identify this and as part of sweep I
41:46 deleted it so I have deleted this
41:49 now here if you see that however memory
41:51 is generally would be like this so one
41:53 data is here one data is here one data
41:55 is here but there are certain memory
41:58 segments which are freed up here
42:00 there are certain memory segments which
42:04 are freed up here so with compaction
42:08 what happen is it will bring the objects
42:13 right it compacted so that we have a
42:16 memories fragment in a sequential form
42:18 later so that it any new object it can
42:20 be added later otherwise it would be
42:22 very difficult to put an object in this
42:25 fragments right so what it will do is it compact
42:27 compact
42:29 the remaining objects which survived and
42:32 so that we have a feed free of space at
42:34 the last and it would be used to put an
42:36 object there
42:38 okay this is known as Mark and sweep
42:40 with compaction
42:43 now there are different versions
42:45 of GC
42:48 first is serial GC
42:49 GC
42:52 second is
42:54 parallel GC
42:59 concurrent mark
43:01 mark
43:02 and sweep
43:19 default version of garbage collector in
43:22 Java 8.
43:25 okay now I'll tell you this is very very
43:27 important understand this serial GC now
43:29 you know that garbage collector is used
43:33 to remove up the memory from the Heap
43:36 from young and old both
43:39 right so serial GC is that only one thread
43:40 thread
43:44 only one thread is used and this thread
43:46 will work let's say garbage collector initiated
43:47 initiated
43:51 and this will work and freed up the
43:53 space only one thread one thread let's
43:56 say so this is called minor GC
43:59 and it is called major GC so in when the
44:01 minor GC will get invoked only one
44:03 thread would be working when major GC
44:05 will invoke only one thread would be
44:08 working only one GC thread so what are
44:10 the disadvantages first of all whenever
44:12 you are only one thread is working it
44:15 definitely work would be slow say it
44:16 would be little bit slow
44:19 second thing is GC is very very expensive
44:21 expensive
44:24 why this is very expensive because
44:27 whenever the GC work start whenever the
44:30 GC work start all application
44:33 application threads
44:35 threads
44:39 will get paused
44:41 so that's why we call that the world
44:45 stop when the garbage collector runs so
44:47 let's say your application is running
44:49 as soon as the garbage collector runs
44:52 your application will become pause now
44:54 garbage collector will complete its work
44:55 and after that it will resume
44:59 so if GC is slow then definitely your
45:02 application would become slow because
45:05 they are pausing too many times
45:07 so that's why GC is very expensive task
45:10 because when the GC work start all
45:13 application thread will pause so this is
45:14 one of the disadvantage of Serial GC
45:17 that only one thread doing the work of cleanup
45:19 cleanup
45:22 so in the Java Aid the parallel GC is
45:25 used in this depending upon the core
45:29 let's say if you have two core CPU four
45:31 core so this many threads got initiated
45:34 for the garbage collector and they work
45:37 parallely right so when there are more
45:39 threats definitely it would be little
45:42 bit fast and there would be less pause
45:44 time but still there is a pause time
45:47 right but only thing is multiple threads
45:50 are working to freed up the space so you
45:52 will get a little bit less pause time
45:55 because the far the work of cleanup
45:58 would be done little bit faster
46:00 but still this is you can say that in
46:03 the latest uh j17 and all
46:06 there has been work has been done to
46:09 further improve this through concurrent
46:10 Mark and sweep
46:12 concurrent Mark and sweeper says that okay
46:13 okay
46:15 while your application threads are
46:18 working this is your application thread
46:21 concurrently GC threads are also working
46:24 so it's a concurrent GC thread so your
46:27 application thread is working and in
46:28 parallel GC thread is also working no
46:30 one is stopping
46:33 so application threads are not stopping
46:37 because of GC threads right but it's not
46:39 100 guarantee
46:41 right jvm doesn't guarantee that your
46:43 application thread will not stop only it
46:46 says that I will try my best that your
46:48 application threads will not stop I will
46:51 try to do concurrent Mark and sweep
46:54 concurrent cleanup I will try to do but
46:56 I don't guarantee hundred percent and
46:58 also there is no memory compaction
47:00 happens so whatever the memory got freed
47:01 up so let's say there is some segment
47:03 Freedom there is certain memory segment
47:06 so no compaction happens in this
47:09 so then there is a further Improvement
47:12 is like G1 so G1 you can say that the
47:13 better version of this where it says
47:14 that okay
47:16 try to give you a hundred percent that
47:18 okay your application thread will not
47:21 stop and also it brings a compaction
47:24 that all the memory is bring together
47:26 and all the freedom memories added at
47:29 last so that they can be used
47:32 so you got it currently Java 8 is using
47:34 parallel GC and there is a further in
47:37 the latest version of java there is a
47:39 further chance that the pause time can
47:41 be reduced
47:45 to a very minimal so that your throughput
47:47 throughput increase
47:49 increase
47:51 and latency decrease when your
47:53 throughput increase means earlier your
47:57 application can process 1000 requests
47:59 per minute let's say
48:02 now because there is less pause time you
48:05 can process let's say 1500 requests per
48:07 minute so your throughput will increase definitely
48:09 definitely
48:11 when what will happen is when your
48:14 throughput increases means you have uh
48:17 threats are not pausing so pause time
48:20 has been removed so definitely your
48:22 latency will go down you will see the advantage
48:24 advantage okay
48:26 okay
48:29 so that's pretty much I have for you in
48:31 memory management and garbage collector
48:33 very very important topic please go
48:35 through one more time you will
48:39 understand it better right and yeah I
48:41 think that's it if any doubt please feel
48:43 free to ping me in the comment section I
48:45 will try to resolve your doubt as soon