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?
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.
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.
The example is:“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”.
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).
Mushfiq Mammadov wrote:Can String literal be eligible for garbage collector in generally?
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”.
Mushfiq Mammadov wrote:It is clear that after first iteration alpha don’t refer “” and maybe “” is eligible for garbage collector.
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).
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).
Mushfiq Mammadov wrote:I understood that neither line1 nor line2 "String literal" will be eligible for garbage collection. Correct?
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?