how to destroy an object in java?(如何在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.
答案应该是e?
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中销毁一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在java中销毁一个对象?


- C++ 和 Java 进程之间的共享内存 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01