• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

String method chaining and garbage collector

 
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 110, in the Method Chaining section this example is given:

And it is written that the above code is equivalent the following code:

Then it is written that “It also creates four String objects..”.

Now it is interesting to know that how many object are eligible for garbage collector in line1, line2 and line3 in the following example?

I think;
in line1 3 object eligible:
"AniMaL ", "AniMaL ".trim(), "AniMaL ".trim().toLowerCase()
in line2 4 object eligible:
adding "AniMaL ".trim().toLowerCase().replace('a', 'A')
in line3 4 object eligible too:
there is no new eligible object, because trim() and toString() return this object.

I don’t know exactly String literal which is kept in String Pool can be eligible for garbage collector or not.
Do I think correct or not in generally? Is it possible such rule?
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:I don’t know exactly String literal which is kept in String Pool can be eligible for garbage collector or not.
Do I think correct or not in generally? Is it possible such rule?


Your thoughts are almost spot-on!

After line1 only the actual result ("AnimAl") is referenced (by variable result), all other temporary strings are eligible for garbage collection. But there's a catch: "AniMaL " is a String literal, so it's in the String Literal Pool and because it's still referenced by the String Literal Pool, it's not eligible for garbage collection. So only 2 String objects will be eligible for garbage collection at line1: the resulting objects from "AniMaL ".trim() and "AniMaL ".trim().toLowerCase()

You were right about line2 (which was not that hard): at line2 the String object referenced by result ("AnimAl") becomes eligible for garbage collection

And you are absolutely correct about the next part: no other objects will be eligible for garbage collection at line3, because always the same object is returned (and thus no new temporary String objects are created).

Although strings and garbage collection can be lots of fun I like to mention this quite important note (from K&B7, page 201)

K&B7 wrote:Note: Due to the vagaries of the String constant pool, the exam focuses its garbage collection questions on non-String objects, and so our garbage collection discussions apply to only non-String objects too.



Hope it helps!
Kind regards,
Roel
 
Mushfiq Mammadov
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote: But there's a catch: "AniMaL " is a String literal, so it's in the String Literal Pool and because it's still referenced by the String Literal Pool, it's not eligible for garbage collection.


Can String literal be eligible for garbage collector in generally? Or this is special case so it's not eligible for garbage collection? Because on page 112 of OCA SE 8 book it is written that

“This sequence of events continues, and after 26 iterations through the loop, a total of 27 objects are instantiates, most of which are immediately eligible for garbage collection”.

The example is:
It is clear that after first iteration alpha don’t refer “” and maybe “” is eligible for garbage collector.

Roel De Nijs wrote: And you are absolutely correct about the next part: no other objects will be eligible for garbage collection at line3, because always the same object is returned (and thus no new temporary String objects are created).


I have learned it from you yesterday
 
Roel De Nijs
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:Can String literal be eligible for garbage collector in generally?


A String literal is always referenced from the String Literal Pool. So there's always an active reference variable to the String object, so it will not be eligible for garbage collection (until of course the program ends and the String Literal Pool is cleared).

Mushfiq Mammadov wrote:Because on page 112 of OCA SE 8 book it is written that

“This sequence of events continues, and after 26 iterations through the loop, a total of 27 objects are instantiates, most of which are immediately eligible for garbage collection”.


The book is spot-on! After that loop, a total of 27 objects are instantiated. And most (not all) of them are immediately eligible for garbage collection. Two String objects will not be eligible for garbage collection: "" (because it's a String literal and referenced from the String Literal Pool) and "abcdefghijklmnopqrstuvwxyz" (the result of the loop and still referenced by alpha).

Mushfiq Mammadov wrote:It is clear that after first iteration alpha don’t refer “” and maybe “” is eligible for garbage collector.


After the 1st iteration alpha doesn't refer to "" anymore, but "" is a String literal and thus referenced from the String Literal Pool, therefore it's not eligible for garbage collection

Hope it helps!
Kind regards,
Roel
 
Mushfiq Mammadov
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:A String literal is always referenced from the String Literal Pool. So there's always an active reference variable to the String object, so it will not be eligible for garbage collection (until of course the program ends and the String Literal Pool is cleared).


I want to ask another question if you don't mind
I understood that neither line1 nor line2 "String literal" will be eligible for garbage collection. Correct?

Roel De Nijs wrote:The book is spot-on! After that loop, a total of 27 objects are instantiated. And most (not all) of them are immediately eligible for garbage collection. Two String objects will not be eligible for garbage collection: "" (because it's a String literal and referenced from the String Literal Pool) and "abcdefghijklmnopqrstuvwxyz" (the result of the loop and still referenced by alpha).


I guessed that all objects are eligible for garbage collection except "abcdefghijklmnopqrstuvwxyz". So I didn't understand why it was written "most of them" instead of "all". Now I learn that "" is not eligible for garbage collection. It is a nice fact.
According this I understand such the string value is not literal if we concat two string with concatenation operator. Correct or not?

P.S. I can't tell my opinion properly in english, sorry for this
 
Roel De Nijs
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:I understood that neither line1 nor line2 "String literal" will be eligible for garbage collection. Correct?


Correct! The string "String literal" will still be referenced from the String Literal Pool and therefore not be eligible for GC.

Mushfiq Mammadov wrote:According this I understand such the string value is not literal if we concat two string with concatenation operator. Correct or not?


Not correct! But that's due another little intricacy about compiler optimization. And that's why garbage collection questions focus on non-String objects. If you compilethe compiler will see you are concatenating 2 String literals and will perform the following optimizationSo it already concatenates both strings, therefore your application will run a tiny bit faster (because the concatenation doesn't have to be executed at runtime anymore). But now the string "It is not literal" will be referenced from the String Literal Pool and therefore not be eligible for GC.

Hope it helps!
Kind regards,
Roel
 
reply
    Bookmark Topic Watch Topic
  • New Topic