Results 1 to 14 of 14

Thread: Garbage Collector

  1. #1

    Garbage Collector

    hi...one interviewer asked me this Q..help me plzzz...
    we can explicitly call the garbagecollector by 3 ways
    1.object.finalise();
    2.system.gc();
    3.Runtime.getRuntime();
    Among these,which one is the most effective one??


  2. #2

    Re: Garbage Collector

    system.gc(); is most effective . and Runtime.getRuntime(); will create runtime variable it will not execute garbage collector method.


  3. #3
    Junior Member
    Join Date
    Jul 2008
    Answers
    2

    Re: Garbage Collector

    system.gc();


  4. #4

    Re: Garbage Collector

    System.gc()


  5. #5
    Junior Member
    Join Date
    Sep 2008
    Answers
    2

    Re: Garbage Collector

    system.gc();


  6. #6
    Junior Member
    Join Date
    Sep 2008
    Answers
    3

    Re: Garbage Collector

    system.gc();


  7. #7
    Junior Member
    Join Date
    Sep 2008
    Answers
    2

    Re: Garbage Collector

    System.gc()

    However, you must know that you can not invoke garbage collector instantly.
    Using System.gc() would place a request to JVM to garbage collect objects.
    JVM would decide according to it's algorithm when to actually do so.
    Further, it is not necessary that all objects eligible for garbage collection would be collected.


    Regards,
    Anand
    j2eeconcepts dot com


  8. #8
    Junior Member
    Join Date
    Oct 2008
    Answers
    3

    Re: Garbage Collector

    Quote Originally Posted by gayathrikundurthi View Post
    hi...one interviewer asked me this Q..help me plzzz...
    we can explicitly call the garbagecollector by 3 ways
    1.object.finalise();
    2.system.gc();
    3.Runtime.getRuntime();
    Among these,which one is the most effective one??
    no one is effective


  9. #9
    Junior Member
    Join Date
    Oct 2008
    Answers
    2

    Re: Garbage Collector

    Quote Originally Posted by gayathrikundurthi View Post
    hi...one interviewer asked me this Q..help me plzzz...
    we can explicitly call the garbagecollector by 3 ways
    1.object.finalise();
    2.system.gc();
    3.Runtime.getRuntime();
    Among these,which one is the most effective one??

    System.gc();

    This is used to invoke the garabagecollector.


  10. #10
    Junior Member
    Join Date
    Oct 2008
    Answers
    2

    Re: Garbage Collector

    which alogrithm used in Thread in java


  11. #11
    Junior Member
    Join Date
    Oct 2008
    Answers
    3

    Re: Garbage Collector

    1.object.finalise();
    Finalizes the object (post mortem clean-up) which means it is ready to be garbage collected..
    Finalization can also be achieved using - System.runFinalization();

    the others 2 and 3 are the same.either of these are favoured over above


  12. #12
    Junior Member
    Join Date
    Oct 2008
    Answers
    1

    Re: Garbage Collector

    system.gc() is the answer suggested for u r question.....


  13. #13
    Junior Member
    Join Date
    Oct 2008
    Answers
    2

    Re: Garbage Collector

    Hey! that is a pretty smart question that got me thinking at first but then I checked
    the jdk documentation of System, Object and Runtime classes. There i discovered this:
    System.gc():-

    public static void gc() {
    Runtime.getRuntime().gc();
    }
    Thus The call System.gc() is effectively equivalent to the call Runtime.getRuntime().gc() which in turn is a native method:-

    public native void gc();

    As for the Object.finalize():-

    protected void finalize() throws Throwable { }

    The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition. So it definitely is not used to call the garbage collector explicitly rather we can override it so that when its called on our object after its been garbage collected, our desired cleanup can be performed.

    So we come down to System.gc() and Runtime.getRuntime.gc() which r effectively the same. Furthermore calling either one does not guarantee garbage collection .

    Regards,
    Neeru


  14. #14

    Re: Garbage Collector

    Quote Originally Posted by gayathrikundurthi View Post
    hi...one interviewer asked me this Q..help me plzzz...
    we can explicitly call the garbagecollector by 3 ways
    1.object.finalise();
    2.system.gc();
    3.Runtime.getRuntime();
    Among these,which one is the most effective one??
    1st option is the best one bcoz it is garbage collectors responsibility


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact