可学答题网 > 问答 > SCJP程序员认证考试题库
目录: 标题| 题干| 答案| 搜索| 相关
问题

How can you force&#8


How can you force garbage collection of an object?()

  • A Garbage collection cannot be forced.
  • B Call System.gc().
  • C Call System.gc(), passing in a reference to the object to be garbage collected.
  • D Call Runtime.gc().
  • E Set all references to the object to new values(null, for example).
参考答案
参考解析:

在Java中垃圾收集是不能被强迫立即执行的。调用System.gc()或Runtime.gc()静态方法不能保证垃圾收集器的立即执行,因为,也许存在着更高优先级的线程。所以选项B、D不正确。选项C的错误在于,System.gc()方法是不接受参数的。选项E中的方法可以使对象在下次垃圾收集器运行时被收集。

分类:SCJP程序员认证考试题库