• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Garbage collection  RSS feed

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




When // doStuff is reached, how many objects are eligible for GC?

A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.

I got the answer b.Please correct me i am wrong
 
Ranch Hand
Posts: 59
Hibernate Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Answer b (2 objects). seems to be the right answer.

Line 10 nulls c2 created on line 9 ( that's 1 object )
Line 11 nulls c1 created on line 8 ( that's 2 objects )
Line 10 assigns null to c3 which was never instantiated,
so.. ( that's still 2 objects, right? )
 
Rick Roberts
Ranch Hand
Posts: 59
Hibernate Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OOPS!

b was not 2 objects.
c was 2 objects... I think 2 objects are available for GC... so answer c.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this thread.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here two objects are eligible for garbage collection.
But which are those two?
At line no 10, no abject is created and null is assign to c3.
At line no 11, one object is eligible for gargbage collection (The one which was created at line 8).
Since CardBoard has one instance variable Short story, which is not reachable, this object also is eligible for garbage collection.
Thus CardBoard and one Short object are eligible for garbage collection.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
Here After line 10 ,the CardBoard objects at line 9 becomes eligible for Garbage Collection since c2 is made to point to null while calling in method go().
At line 11 ,the CardBoard object present in line 8 becomes eligible.
Hence two cardboard objects becomes eligible for Garbage Collection.

I dont understand why Short object story becomes elligble for Garbage collection.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think only object referenced by c1 will be eligible for garbage collection.

Since parameters are passed by value, c2 in main will not be made null, so this object is not available for garbage collection.

No object was created and referenced by c3, it was just initialized to null.

So only 1 object should be eligible for garbage collection.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Rajesh Kadle:
I think only object referenced by c1 will be eligible for garbage collection.

Since parameters are passed by value, c2 in main will not be made null, so this object is not available for garbage collection.

No object was created and referenced by c3, it was just initialized to null.

So only 1 object should be eligible for garbage collection.



Correct.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always modify the code to get an idea what is happening. But realize that System.gc may not always do what you want it to.
 
Riyaz Saiyed
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not satisfied.
This is the same example, sited in the K & B, SCJP 5 book (Page 266).
It says..
Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!