Home / Java Programming / Garbage Collections :: Discussion

Discussion :: Garbage Collections

  1. Which statement is true?

  2. A.
    Memory is reclaimed by calling Runtime.gc().
    B.
    Objects are not collected if they are accessible from live threads.
    C.
    An OutOfMemory error is only thrown if a single block of memory cannot be found that is large enough for a particular requirement.
    D.
    Objects that have finalize() methods always have their finalize() methods called before the program ends.

    View Answer

    Workspace

    Answer : Option B

    Explanation :

    Option B is correct. If an object can be accessed from a live thread, it can't be garbage collected.

    Option A is wrong. Runtime.gc() asks the garbage collector to run, but the garbage collector never makes any guarantees about when it will run or what unreachable objects it will free from memory.

    Option C is wrong. The garbage collector runs immediately the system is out of memory before an OutOfMemoryException is thrown by the JVM.

    Option D is wrong. If this were the case then the garbage collector would actively hang onto objects until a program finishes - this goes against the purpose of the garbage collector.


Be The First To Comment