Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Garbage Collection!

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects are eligible for garbage collection after this for loop?
for (int i = 0; i < 10; i++) {
q1 = new Q(i); // 1
}
I thought it's 10.Can anybody help pl!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens after the end of the loop! What's the scope of q1!
Hope that helps!
Barry!
Please don't put ! after every subject! After a qustion use ? not ! !

Nobody can hear you here so don't shout!
[ May 11, 2004: Message edited by: Barry Gaunt ]
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if q1 is initiated within the loop, then the answer is 10.If it is initiated outside the loop, the answer is 9.Am I rite?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the end of the loop, q1 holds the reference to the last object allocated thus making it ineligible for GC.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is where is q1 defined? Not where it is initialized or given subsequent values. q1 is clearly defined out side the loop, and as David says, it will still contain a reference to a Q object (new Q(9)) after the loop exits.
 
reply
    Bookmark Topic Watch Topic
  • New Topic