Win a flower (🌹) or copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java (📚) this week in the Agile and Other Processes forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
stewards:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Garbage Collection

 
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a mock question:

Here is a method which creates a number of String objects in the course of printing a count down sequence. When the program reaches line 8, how many of the String objects created in line 5 are eligible for garbage colloection?



A. none
B. 1
c. 10
D. 11

My take is D, but the answer is C. The explanation is because only the last String object of the 11 created still has reference.

My question is the tmp variable is difined in the for loop. When out of the loop, tmp will not exist. How can it hold the last object?

Thanks as always.
 
pioneer
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with D

By line 8, the String reference tmp is out of scope and all 11 Strings are unreachable.
 
pioneer
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OTOH they could have been placed in the pool, which causes them them to not be subject to normal garbage collection...

The real exam won't therefore present questions about GC and String (at least that's what other tell me).
 
pioneer
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the variable go out of scope when it exits the loop or when it exits the method ? I remember having read somewhere, in javaranch, that objects are available for garbage collection not immediately, but only when they come out of the method.
Please correct me if otherwise.
 
PNS Subramanian
pioneer
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to add on to my previous post, in this scenario i believe there would not be any garbage collection taking place since the objects do not get created in the heap. This was tested through JProbe.
 
Jeroen Wenting
pioneer
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever there are no more references to an object does it get eligible for garbage collection.
For objects that don't loose a reference inside a method, this happens when the method terminates (unless an object is in the method placed in a collection that has a reference outside the collection or the object is returned from the method, in which case the object will survive the method.
 
Mike Gershman
pioneer
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the given code, tmp is a local variable in the block which is the body of the for statement. When the loop ends and line 8 is reached, the variable is out of scope.

As for String objects going on the heap, the only time we know they are unique objects on the heap is when new is used.

The only times we know the String objects are not on the heap are when a String literal or other constant expression is used, when a String method is used on a String object in the constant pool and no change to the String object occurs ("abc".trim()), and when String.intern() has been used on a String object.

The above guaranteed cases are fair game for the exam, more likely using the == operator than using System.gc(). That leaves many cases that are implementation dependent and therefore should not be on the SCJP exam.
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!