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

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

问题描述

在接受以下选项的采访中遇到此问题:


如何在java中销毁对象?



  a。的System.gc(); 
b。 Runtime.getRuntime.gc();
c。 object.delete();
d。 object.finalize();
e。 Java自己执行gc,不需要手动执行。




  1. 答案应该是e?

  2. 如果e不在那里,该怎么办?然后 ?
    显然不是答案。 a和b将为整个应用程序执行gc(问题需要一个对象)。
    我认为它是d,因为finalize()在gc之前被调用(但是在调用gc之后是否有必要)?或者我错了? e必须在那里回答这个问题吗? 回答。如果E不存在,您很快就会用完内存(或)没有正确答案。



    对象应该无法访问以符合GC要求。 JVM将执行多次扫描并将对象从一代传送到另一代,以确定GC的合格性,并在对象无法访问时释放内存。


    I encountered this question in an interview with following options:

    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. The answer should be e?

    2. 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 ?

    解决方案

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

    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天全站免登陆