This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

GC Q's from Javeed�s Mock Exam Page

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
9. True of False
You can invoke the garbage collector whenever you want
a] True
b] False
The answer given is b false, but i think is a true, because you can atleast invoke the garbage collected whenever you want but doesn't mean it will run.
Please clarify. As i am getting confused, on similar questions.
Regards
Sharmila
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sharmila,
You cannot force the JVM to garbage collect an object by
invoking a call to <code> System.gc() </code> or a call
to <code> Runtime.gc() </code>.
To quote the JAVA API clearly states the fol:


gc
<code> public static void gc()</code>
Runs the garbage collector.
Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
The call <code> System.gc() </code> is effectively equivalent to the call:
<code> Runtime.getRuntime().gc() </code>


The best you can do is to speed-up the gc
is by setting the object references to <code> null </code> when you
no longer require them. Doing so will ensure that memory leaks
dont exists and suggest the JVM's garbage collector to take steps to speed-up the garbage-collection process. Whe the garbage collector finds that memory that is no longer accessible from any live thread, it takes steps
to release the memory back into the heap for re-use.
Hope this handles your querry.
Please revert in case of any further clarification on this.
Ravindra Mohan


[This message has been edited by Ravindra Mohan (edited May 02, 2001).]
 
Sharmila Abkari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ravindra,
but isn't System.gc called as invoking the constructor.
So shouldn't the answer to that question be true.
I understand that this will not necessary mean that gc takes place. But atleast we can invoke the constructor.

Regards
Sharmila
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sharmila,


but isn't System.gc called as invoking the constructor.
So shouldn't the answer to that question be true.
I understand that this will not necessary mean that gc takes place. But atleast we can
invoke the constructor.


Let me clarify here. <code> gc</code> is not a constructor, rather is a <code> static method of "java.lang.System" class
</code>.
Thats the reason you dont need to create an instance of <code> System class </code> but can call the method directly by giving its "classname".method. I hope this clarifies the confusion in your mind.
Rgds,
Ravindra Mohan

[This message has been edited by Ravindra Mohan (edited May 03, 2001).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sharmila, Ravindra
You're right; the word 'invoke' normally means 'call' ie you can invoke the System.gc() method or you can call the System.gc() method.
I would also have responded 'true' to this question, based on the wording. If it said 'you can run the garbage collector whenever you want' then the answer would be false; since, as Ravindra pointed out, calling the method simply sends a request to the gc, asking it to run...it doesn't force the gc to run.
The wording on the real exam is very clear and straight forward so you shouldn't run into this type of problem. If you see the word 'invoke' it invariably means 'call'.
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic