Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg. GC

 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is a question from Barry Boone's exam :
Which statements about garbage collection are true?
Select all valid answers.

a.You can directly free the memory allocated by an object.
b.You can directly run the garbage collector whenever you want to.
c.The garbage collector informs your object when it is about to be garbage collected.
d.The garbage collector reclaims an object�s memory as soon as it becomes a candidate for garbage collection.
e.The garbage collector runs in low-memory situations

The correct answer is b,c,e.
But i am not clear about the statement c.
Can someone explain it to me.
 
james hoskins
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello angela,
gc informs an object of its imminent demize by calling is finalize() method.
kind regards,
james hoskins
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there some internal mechanism by which it informs the object
that it is going to be gced.

 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How come b is correct?
--Farooq
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you Muhammad, I think we are faced with a poorly worded answer again... b would be correct if the answer would be "You can directly INVOKE the garbage collector whenever you want to."
Though no warranty is given whether the gc will be run directly or not !
Val
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Val, but ur response creates a confusion in my mind, when we say invoke, doesn't it mean run. I think all we can do is to try to run or try to invoke, all we can't do is to force to do it.
--Farooq
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Invoke means actually call a method upon an instance of an object, like this
String s = "String1";
s.trim();
here you invoke the method trim upon instance s.
Concerning the gc, you invoke gc() upon the class System. What System.gc() does is invoking gc() on an instance of class Runtime (Runtime.getRuntime().gc()). Note that the method gc in class Runtime is native. It is up to the method gc() to actually try to make the garbage collector run, but nothing is guaranteed. It is not because you "invoke" a method that the body of that method will be sequentially executed.
Another example is the method start() of the class Thread. When you "invoke" the method start() on an instance of a Thread, the method returns almost immediately even if the Thread instance is not started yet.
All this to say that "invoking" the method gc() does not mean "running" the garbage collector, but just that "you'd like the garbage collector to run now".
let me know Muhammad if you are still confused !
Val
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Val.
--Farooq.
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the below two statements :

1. You can call the garbage collector whenever you want to. (True/False )
2. You can directly run the garbage collector whenever you want to.
(True/False)

According to me :
1. This statement is more like the statement "Garbage collection can be forced" which should be false.
2. Is this statement similar to what we mean by making a call
System.gc() ? But then it should have been donot have been "invoke"
instead of "run"
Pls. explain..I am still not clear .

 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. you can call gc means you can invoke gc which is true
2. you can run means than if you invoke the gc then it will immediately run, which is false
Val
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic