OCJA(1Z0 - 808) || String Objects Creation Heap and String Constant Pool (SCP) Part - 1
Summary
TLDRThis video explores key differences between creating String objects in Java using `new String()` and string literals. It explains that using `new String()` creates two objects—one in the heap and another in the String Constant Pool (SCP)—while a string literal creates only one in the SCP. The video also delves into JVM memory management, garbage collection, and how objects in the SCP are reused for efficiency. The session clarifies how the heap and SCP handle string objects differently, ensuring memory optimization and object reuse.
Takeaways
- 📜 String object creation depends on the use of the 'new' keyword, and it leads to different memory allocation behaviors.
- 🏗️ When 'new String()' is used, two objects are created: one in the heap and one in the String Constant Pool (SCP).
- 💡 In contrast, without 'new', only one object is created in the SCP, as JVM reuses the object if it already exists.
- 🗂️ The purpose of the SCP is to allow the JVM to reuse objects, improving memory efficiency.
- 🗑️ Objects created in the SCP are not eligible for garbage collection since JVM keeps an implicit reference to them.
- ⚠️ Until Java version 1.6, the SCP was part of the method area or permgen space, but from Java 1.7 onwards, it moved to the heap for better memory management.
- 🔄 In cases where string literals are already present in the SCP, JVM reuses them instead of creating new objects, enhancing efficiency.
- ⚙️ When runtime operations modify strings, new objects are always created in the heap, not the SCP.
- ❌ Objects without explicit reference in the heap are eligible for garbage collection, unlike those in the SCP.
- 💾 Using 'new' ensures a new object is created in the heap even if an identical object exists in the SCP, avoiding reuse in the heap.
Q & A
What is the difference between using 'string s = new string("druga")' and 'string s = "druga"'?
-Using 'string s = new string("druga")' creates a new object in the heap memory each time, while 'string s = "druga"' utilizes the string literal pool in the string constant pool (SCP), reusing the same object for the same literal, thus saving memory.
Why is it said that using the 'new' operator is dangerous in the context of strings?
-Using the 'new' operator for strings can lead to unnecessary object creation in the heap, which might not be garbage collected immediately due to implicit references maintained by the JVM, leading to increased memory usage.
What is the role of the string constant pool (SCP) in Java?
-The string constant pool (SCP), also known as the string intern pool, stores literals to ensure that each string literal is unique in memory, thus saving space and improving memory utilization.
How does the JVM handle string literals?
-The JVM checks if a string literal already exists in the SCP before creating a new object. If it exists, it reuses the existing object; if not, it creates a new object and adds it to the SCP for future use.
What happens to the object created with 'new string("druga")' in the SCP?
-When a new object is created using 'new string("druga")', a copy is also maintained in the SCP for future references, but the original object in the heap is not eligible for garbage collection due to implicit references by the JVM.
Why are objects created with string literals not immediately eligible for garbage collection?
-Objects created with string literals are not immediately eligible for garbage collection because the JVM maintains an implicit reference to them in the SCP, ensuring they are available for future use.
What is the impact of Java version on how the string constant pool is managed?
-Prior to Java 7, the SCP was part of the permanent generation in the heap. From Java 7 onwards, the SCP is part of the heap itself, allowing it to expand and contract as needed for efficient memory utilization.
Can you provide an example of how many objects are created with 'string s1 = new string("druga"); string s2 = "druga";'?
-In this example, three objects are created: one in the heap for 's1', one in the SCP for the literal 'druga', and another in the heap for 's2' if it's a new object not found in the SCP.
What is the significance of the string pool in memory management?
-The string pool is significant in memory management as it prevents the creation of duplicate string objects for identical literals, thus conserving memory and enhancing performance.
How does the JVM decide whether to reuse an object from the SCP or create a new one?
-The JVM checks if the content of the string literal already exists in the SCP. If it does, it reuses the existing object; if not, it creates a new object and adds it to the SCP for potential future reuse.
What is the difference between the heap area and the string constant pool (SCP) in terms of object creation?
-The heap area always creates a new object when using the 'new' operator, whereas the SCP may reuse existing objects for identical string literals, thus optimizing memory usage.
Outlines
🔍 Understanding String Object Creation and the 'New' Operator
This paragraph delves into the differences between creating string objects using the 'new' keyword and direct string assignment in Java. It explains that when using 'new String()', two objects are created—one in the heap memory and one in the String Constant Pool (SCP). In contrast, when directly assigning a string, only one object is created in the SCP. The paragraph emphasizes the JVM’s role in reusing existing SCP objects for optimization and avoiding duplicate object creation.
🧠 JVM's String Reuse Mechanism in SCP
This section continues the discussion on string object creation but focuses on how the JVM checks if an identical object already exists in the SCP before creating a new one. If an object with the same content is found, it is reused instead of creating a new one. Additionally, it highlights that string literals always create objects in the SCP and that objects created via 'new' will exist in the heap memory, with SCP used for future reuse.
🔄 Memory Allocation in Heap vs SCP with Multiple Strings
The paragraph uses multiple string creation examples to explain how the heap and SCP store string objects. It illustrates that creating strings with 'new' leads to new heap objects, while direct string assignment results in objects being checked for reuse in the SCP. The JVM optimizes memory by ensuring no duplicate objects exist in the SCP, but duplicate heap objects can exist when 'new' is used repeatedly with the same string content.
💡 Detailed Example of String Object Creation and Reuse
The final paragraph further explains string object creation through an example where multiple strings are created with the same content. It shows that each 'new String()' creates separate objects in the heap, but SCP objects are reused. It also touches on garbage collection, stating that objects without references in the heap are eligible for garbage collection, while SCP objects are maintained for future reuse by the JVM. The paragraph concludes by encouraging readers to continue learning through examples to fully grasp the heap and SCP concepts.
Mindmap
Keywords
💡String Literal
💡Heap Memory
💡String Constant Pool (SCP)
💡Garbage Collection
💡New Operator
💡Immutability
💡Memory Management
💡Runtime Operation
💡Reference Variable
💡Efficiency
Highlights
Understanding the difference between 'new String()' and string literals is crucial for Java memory management.
In the case of 'String s = new String("Durga")', two objects are created: one in the heap and one in the String Constant Pool (SCP).
When using the 'new' operator, a new object is always created in the heap, regardless of existing objects in the SCP.
For string literals like 'String s = "Durga"', only one object is created in the SCP if it doesn't already exist.
The SCP is used for memory optimization, ensuring that identical string literals are reused rather than recreated.
From Java 1.7 onwards, the SCP is moved to the heap for more efficient memory utilization.
Objects in the SCP are not eligible for garbage collection because they are implicitly referenced by the JVM.
Using 'new String()' forces the creation of a new object in the heap, even if the string already exists in the SCP.
Without the 'new' keyword, the JVM first checks the SCP for existing objects with the same content and reuses them if available.
When a string is created using 'new', two objects may exist with the same content—one in the heap and one in the SCP.
Strings are immutable in Java, so any modification results in the creation of a new object in the heap.
Runtime operations that modify a string do not affect objects in the SCP, only creating new objects in the heap.
The JVM manages memory efficiently by maintaining implicit references to objects in the SCP, ensuring they aren't collected by the garbage collector.
For every string literal, the JVM creates only one object in the SCP for reuse across the program.
Understanding the interplay between the heap and SCP is key to managing memory effectively and avoiding unnecessary object creation in Java.
Transcripts
[Music]
you
in the last two videos we covered about
immutability mutability equals method
what is the difference between sting and
sing before okay up to that clear now
the next case is very important case
right sir if I can't a string yes is
equal to new string of juga string yes
he's equal to new string of druga next
string yes is equal to du --ga what EDA
difference between these two things can
you please tell the most dangerous point
string s is equal to new sting of druga
string s is equal to draaga what is the
difference between these two things
friends the most dangerous concept next
da almost up another discussion based on
this example only you require two lesion
very very carefully sir can you please
tell what the difference between these
two things
swinging a silicon using of druga
stingiest is equal to druga remember
that in the posture case okay in the
posture case two objects will be created
but in the second case only one object
will be created remember that in the
posture case two objects will be created
but in the second case only one object
will be created how we read are very the
difference it's not like do you know
whenever we are using new operator
whenever we are using new operator
compulsory universe it will be created
in the hip area okay Veta contain two
Duga and ax you see the reference
variable for this forgetting
inter-country
Duga you see the reference variable for
this next our new string of druga new
string of droga sorry it's a little
bright for you first string the literal
for the future purpose okay one object
will be created in SCP area forgetting
what is that yes
P string a constant a pool remember this
one what it is yes it is a string a
constant the pool okay you new object
will be created in the ACP area survey D
the reference variable no explicit
reference variable but internally
implicit reference variable will be
maintained by the JVM can immediately
Mosca serve I did the deed of this one
the need of this one is reusing same
object for future purpose are getting so
two objects will be created one is in
the hip area
yes he's pointing to that heap object
another copy will be created in ACP area
for the future purpose sir water future
purpose simple point you should have a
reuse operating in the next level
examples you can able to get okay well
then immediately Mosca sir this object
this object is available for garbage
collection not not no no because
implicitly implicitly reference variable
will be maintained by JVM if any object
created because of because I'm string
the constant Litoral Litoral Litoral
implicit reference variable will be
maintained by JVM this object is not
eligible for garbage collection remember
that but immediately you may ask her
suck this year CP v r TC area is there
until 1.6 version until 1.6 version yes
CP it apart
ah sir me that a year yeah method again
yeah yes if it apart of method area are
permit Jen are getting like permanent
permanent generation okay hit the bottom
method area are permit Jen okay like
buttah from one point seven version
onwards for efficient memory utilization
sir ACP area is moving to keep only okay
in the hip area some water reservoir for
SCP okay in the in the permian do you
know meter area yes CP zombies fixer
sizes but once it is more to the paper
no my ACP can
expandable regarding so just because of
memory utilization you can just stuff
from 1.7 version and word this SCP is
move order to okay apparently remember
but whoever Chan it is a part of me that
area but not inside hip area
okay well sub this is just for internal
sake but anyway you are not required to
worry so here how many objects you got
can you attend two objects got created
one is in the hip area second one in the
ACP yes he is always pointing to that
hip area right so quietly subject to
play Sarah this object up for future
purposeful what is that future purpose I
will explain in the future don't worry
about that okay well but in this case
string EEOC's equal to du gard easy
collateral or not yes it's a literal
it's an apron
something you know in this case in this
case only one object will be created in
the SCP year yeah in this case only one
object will be created in SCP year yeah
remember this concern in the ACP only
one object will be created you see the
reference point will do that but anyway
mistake JVM will check is there any
object the content with this content
already available in there CPR now if it
is already there if it is already there
then only then only so then it is going
to reuse the same object if the object
is not already there then only the new
object will be created and yes he's
pointing to that but anyway object at
creation in the hip area is always
mandatory whenever we are using new
operator compulsory a new object will be
created but uh if you are going to
create like this so object creation is
not mandatory pasta JVM wheelchair in
the SCP area do you have any object
within this content or not if the object
is already there ryuuta same object so
if the object is not already there then
only create a new object that's all so
what is the difference between this one
under this one UD scare total hominy
object you are created to object will be
created under in these case how many
objects will be created sir only one
object that
in the SCP area that's not no sir why it
is a second object placed in SCP because
the reason is for future purpose flour
we use reusable deep purpose look at
this object not having any reference
easy to people forgot my collection
aetna sorry it is not visible for
garbage collection because internal
difference will be maintained by JVM
itself clear sir in the last example
just we had a discussion about the keep
and ACP object creation let me continue
some more example so that you people can
get much cloud directive here observe
I'm taking string s1 is equal to new
string off Duga string s1 is you got you
still got broken string yes 2 is equal
to new string ah-oogah string s2 is you
continue single Turki next string yes
sir 3 is equal to-- daruka string s 3 is
equal to Trueba string ha yessuh 4 is
equal to 2 string s4 is equalto troopa
Tata
can you please tell just because of this
line how many objects would got created
yeah respond just because of this line
how many objects have got could get it
how many are there in the general heap
how many are there in the SCP yet done
please confirm total ammonium Jake's got
created is it only one object to object
R 3 R 4 R 5 are getting R 5 r 6 how many
how many will be there in the hip area
how many will be there in SCP area yes
if we want to be have some guess in your
mind so that you can able to understand
dancer very clearly if I start
explaining
can you please guess how many up there
in the hip area - how many are there in
their City area huh remember okay just
let me start explanation so that you
people can feel
more comfortable right here do you know
after first align deep area SCP area no
legend legend there are some conclusions
also I want to talk string s1 is equal
to new singer Drogo
whenever we are using new operator sorry
any website will be created in the hip
area
remember compulsory you new object will
be created in the hip area
that's why Durga Durga yes someone is
pointing to him next
it is a literal literal right for the
future purpose one copy will be
maintained in the ACP area
yes CP area right okay been sub just
took because a post line how many
objects got to get it to objects got
created one is in the hip area second
one in the ACP area no string s2 is
equal to neo stink after ooga singing
STDs you called me on string of Durga so
now whenever we are using new operator
come on sir can you object will be
created sir in the hip area can you
watch it will be created
yes - next ah this a copy has to place
in the ACP but in ACP Durga contain all
of the object is available or not yes if
it is already there we are not required
to place remember this one already care
we are not required to place okay second
line also completed trinka yes sir three
is equal to do god where I require to
create the subject
I told already in the last example where
I record a cricketer subject in the SCP
area but in ACP area object creation is
always optional posture JVM wheelchair
is there any object already already
pointing already not having the same
content or not if any object already
having the same content then existing
object will be reused it won't create a
new object that's why string yes a three
yes a three is also pointing to druga
string yes four is equal to druga yes up
for is also pointing to the graph
yes four is also fine filter Durga one
day now can you please confirm
total total sir how many objects got
created only three objects got created
one is in the ACP area two objects in
the heap area right so the conclusion is
whenever we are using new operator
compulsory you watch it will be created
in the hip area that's why there may be
a chance of existing two objects are
with the same content in the hip area
yes Isidro god raga sim kundan de ba
there is no chance of existing two
objects with the same content in the
ASAP area remember these ones are in the
ICP area with CM contain only one object
will be there same object will be reused
multiple times so internally memory will
be C over memory utilization will be
improve all right clear now you for
knowledge about ACP on the heap from
zero to twenty percent price I'm sure if
I will go for two more two or three more
example then you will get much clarity
don't require to worry to this clear so
the next example related to heap and ACP
I will upon sir string s is equal to new
string of druga string is equal to
string of draaga yes sir doctor can get
up software yes sir
is equal to your start can't get up
solutions okay well can you please tell
total how many objects will be created
in the hip area how many there sa P area
how many there
can you please yes friends can you
please tell
total how many objects will be created
how many present in the hip area how
many present in SCP area yes okay well
so compulsory you people should know
some answer in your mind okay maybe 3 4
2 4 or 6 for something like you should
have some clarity okay
now have a look once
period general heap and the SCP area
poster line string a city called new
string of Duga what this one not
required to keep any explanation even in
the last two examples also we covered
yes is equal to using of druga one copy
will be created in the hip area for the
future purpose another copy will be
created in SCP area
okay well Duga like this yes sir da can
get all software yes a dot can get our
software software like this I'm taking
some do you know yes sir that can get up
software here abjure software easy a
string literal software is a string a
little later but if restring a literal
one copy will be created in the string
concerned full because the name a we
observed string constant a pole
it is doing a constant string electro
means a string constant for a first
finger constant one copy will be created
in the SCP area that's why a faster
software will be created in the SCP area
ok software got created in ACP area okay
Ben next yes
do you know so yes sir doc can get up
software already what is yes yes it
druga container software
draaga software do you know so string is
immutable
you can't change the content if you are
trying to perform the change compulsory
new object will be created
do you know because of runtime operation
may be made that a call because of
runtime operation if an object is
required to create that option is always
going to create only in the hip area but
not in ACP remember this one sir
sir I call can get because of that
univox it will be created
runtime operation if an object God
created that the new object will be
created only in the hip area but not in
ESCP yes see Pimenta far
constants string literals ax not because
of from running operation created
objects so now yes Eric can get up
software
yes he's already dooga doo Ruger
software
yaniv object got created forgetting
durga software any website God created
right okay
for this a new object do you have any
reference variable no no no we are not
assigning to any reference variable
that's fine
this object eligible for got pay
collection okay anyway it is eligible
for GC next ah yes is equal to yes it
are can get our solutions is here
yes rot can get our solution solution
solutions are is a constant string
constant compulsory one copy will be
there in the SCP year yeah okay
solutions are easier contain easy easy
one copy will be there in the SCP area
because it's a string electron why for
every single a troll one copy will be
created in a city for the reusable D
purpose next time yes sir the arc can
get absolution because he is always
pointing to druga durga that target of
solutions
now Duga solutions will be created a new
object because of runtime operation if
an object is required to create that
object is always going to be placed only
in the area next uh DISA resulta
we are assigning to yes here I 70s now
onward this yes sir is pointing to this
object
regarding a 1 watt TCS is pointing to
this object if you print yes happily
your content through girl solutions
that's all can you please tell total how
many objects were created how many there
in the hip area how many there in the
SCP six up chicks got created because of
the score three present in the hip area
three present in the ASAP under these
are two objects eligible for garbage
collection only these objects not
eligible for garbage collection in the
heap anyway these are not eligible for
DC because implicit reference variables
will be maintained by J
YUM now are you getting clarity okay I
hope but just to take a bit special care
so for every example you are going to
learn some new thing maybe after one not
pre-exam 'pl yes all the things are
going to be completed then you will get
much clarity about this concept clear
for love read but keep on listening very
important
[Music]
5.0 / 5 (0 votes)