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中销毁一个对象?
基础教程推荐
- Maven:无效的目标版本:10 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
