wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Qustion about Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Qustion about Garbage Collection" Watch "Qustion about Garbage Collection" New topic
Author

Qustion about Garbage Collection

Aiglee Castillo
Greenhorn

Joined: Aug 10, 2006
Posts: 27
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!!
Zip Ped
Ranch Hand

Joined: Jul 26, 2005
Posts: 336
Variable i is a local vaiable in the for block. The variable becomes inaccessible once the block is over.
Aiglee Castillo
Greenhorn

Joined: Aug 10, 2006
Posts: 27
exactly, would not be garbage collected too?
Rachil Chandran
Ranch Hand

Joined: Mar 05, 2006
Posts: 67
'i' is a primitive and disappears after the the stack for the block is erased


SCJP2 96%
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
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() ?
ramesh subramani
Greenhorn

Joined: Jun 26, 2006
Posts: 1
After making array=null all its elements hold null, so the answer should be 4??
[ August 22, 2006: Message edited by: ramesh subramani ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Aiglee Castillo
Greenhorn

Joined: Aug 10, 2006
Posts: 27
Ohh!

Thank you!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Qustion about Garbage Collection
 
Similar Threads
Garbage collection
garbage collection
Objects eligible for GC
Garbage Collection
Garbase Collection Help