• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Qustion about Garbage Collection

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I just did a practice test of the Whizlabs SCJP 5.0 Exam Simulator and I'm not sure about this question:

How many objects are eligible for garbage collection when the System.gc() method is invoked?


A) 3
B) 4
C) 5
D) Code does not compile
E) None of the above

It says the answer is A, because the array, but what about the variable i of the loop??

Thanks in advance!!
 
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variable i is a local vaiable in the for block. The variable becomes inaccessible once the block is over.
 
Aiglee Castillo
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
exactly, would not be garbage collected too?
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'i' is a primitive and disappears after the the stack for the block is erased
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Aiglee Castillo:
It says the answer is A, because the array, but what about the variable i of the loop??



Only "Objects" are Garbage-collected. Not the variables. (Variables hold either references to objects or values of primitives)

to put in other words, only those things are GC'ed which are created by using "new" operator. (Lets keep String literals aside) In your code, did you create "i" by saying something line new i() ?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After making array=null all its elements hold null, so the answer should be 4??
[ August 22, 2006: Message edited by: ramesh subramani ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ramesh subramani:
After making array=null all its elements hold null, so the answer should be 4??

[ August 22, 2006: Message edited by: ramesh subramani ]



No, because the variable array being set to null is the local variable in method m.
The varable array in method main still holds a reference to the array.
[ August 22, 2006: Message edited by: Barry Gaunt ]
 
Aiglee Castillo
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh!

Thank you!!!
 
There is no "i" in denial. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic