如何在java中销毁一个对象? [英] how to destroy an object in java?

查看:31
本文介绍了如何在java中销毁一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一次采访中遇到了这个问题,有以下选择:

I encountered this question in an interview with following options:

如何在java中销毁一个对象?

How to destroy an object in java?

a. System.gc();  
b. Runtime.getRuntime.gc();  
c. object.delete();  
d. object.finalize();  
e. Java performs gc by itself, no need to do it manually.

  1. 答案应该是e?

  1. The answer should be e?

如果没有 e 怎么办?然后 ?显然 c 不是答案.a 和 b 将为整个应用程序执行 gc(问题需要一个对象).我认为它是 d 因为 finalize() 在 gc 之前被调用(但是在 finalize gc 被调用之后是否有必要?)或者我错了?e 必须在场才能回答这个问题?

what if e was not there? then ? clearly c is not the answer. a and b will do gc for the whole application(question requires for one object). I think it is d because finalize() is called just prior to gc(but is it necessary that after finalize gc is invoked ?) or I am wrong ? e must be there to answer this question ?

推荐答案

答案 E 是正确答案.如果 E 不存在,你很快就会耗尽内存(或)没有正确答案.

Answer E is correct answer. If E is not there, you will soon run out of memory (or) No correct answer.

对象应该是不可访问的,才有资格进行 GC.JVM 会进行多次扫描并将对象从一代移动到另一代,以确定 GC 的资格,并在对象不可达时释放内存.

Object should be unreachable to be eligible for GC. JVM will do multiple scans and moving objects from one generation to another generation to determine the eligibility of GC and frees the memory when the objects are not reachable.

这篇关于如何在java中销毁一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆