强制设置某个对象为null 真的有用吗?

amdgaming 2011-03-01 06:26:39
很多人写代码 习惯 把不使用的对象设置为null
比如:

Object obj = new Object ();

obj = null;

这样真的能加快垃圾回收吗?

还有如果文件流 关闭失败,会引起内存泄漏吗?
...全文
584 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
i李小武 2011-03-02
  • 打赏
  • 举报
回复
总结一下:
1.一般不会加速垃圾回收,因为回收是不定期的。
2.可能导致内存泄漏,文件流关闭失败。
江郎才近 2011-03-02
  • 打赏
  • 举报
回复
不能加快,但是很多面试官喜欢看这种代码,他觉得你能写出这种代码至少能够证明你有个好的编程习惯。特别是在面试关于流的操作的时候如果你在调用流的close方法后再把这个对象赋值为null那么面试官将会在心中给你加分。
wsxiapiaoxue 2011-03-02
  • 打赏
  • 举报
回复
设置成null以后,就相当于告诉JVM此空间已经空了,如果不设置,则需要等到程序结束时JVM才去判断对象是否销毁
beowulf2005 2011-03-02
  • 打赏
  • 举报
回复
http://www.javaspecialists.co.za/archive/Issue060.html

http://www.ibm.com/developerworks/java/library/j-jtp01274.html
beowulf2005 2011-03-02
  • 打赏
  • 举报
回复
设成null会加快垃圾回收的速度,
也依赖垃圾收集器的具体实现,
在某些JVM 可能没有太大作用。
nulling 做的太过分的话,甚至会托慢有些垃圾收集器。

总之,没大用。
Mybeautiful 2011-03-02
  • 打赏
  • 举报
回复
没有用.
sevendawn 2011-03-02
  • 打赏
  • 举报
回复
也是当成习惯了,应该能吧
keeya0416 2011-03-02
  • 打赏
  • 举报
回复
Object obj = new Object ();

obj = null;

我觉得这是在obj的有效域结束之前就告诉 jvm 这个 obj 我后边不会再用了
ZangXT 2011-03-02
  • 打赏
  • 举报
回复
推荐一下《Effective Java》,里面有个Stack的例子,需要设置为null,一般不需要。
安卓机器人 2011-03-02
  • 打赏
  • 举报
回复
设置成null是为了让JVM优先选择回收
ZangXT 2011-03-02
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 wsxiapiaoxue 的回复:]

设置成null以后,就相当于告诉JVM此空间已经空了,如果不设置,则需要等到程序结束时JVM才去判断对象是否销毁
[/Quote]
起码SUN的JVM不会等程序结束……
beowulf2005 2011-03-01
  • 打赏
  • 举报
回复
设成null会加快垃圾回收的速度,对垃圾回收的间隔应该没有大影响。
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 amdgaming 的回复:]
Java code

还有如果文件流 关闭失败,会引起内存泄漏吗?

这个问题 请大家也回答一下。谢谢
[/Quote]

见过有些文件无法删除么?说什么这个文件正在被另一个程序使用。

关闭失败的话,那说明这个文件出问题了,无法关闭的文件通道或者这个文件通道已经被关闭,因此不会产生内存泄漏。
dracularking 2011-03-01
  • 打赏
  • 举报
回复
It depends a bit on when you were thinking of nulling the reference.

If you have an object chain A->B->C, then once A is not reachable, A, B and C will all be eligible for garbage collection (assuming nothing else is referring to either B or C). There's no need, and never has been any need, to explicitly set references A->B or B->C to null, for example.

Apart from that, most of the time the issue doesn't really arise, because in reality you're dealing with objects in collections. You should generally always be thinking of removing objects from lists, maps etc by calling the appropiate remove() method.

The case where there used to be some advice to set references to null was specifically in a long scope where a memory-intensive object ceased to be used partway through the scope. For example:

{
BigObject obj = ...
doSomethingWith(obj);
obj = null; <-- explicitly set to null
doSomethingElse();
}

The rationale here was that because obj is still in scope, then without the explicit nulling of the reference, it does not become garbage collectable until after the doSomethingElse() method completes. And this is the advice that probably no longer holds on modern JVMs: it turns out that the JIT compiler can work out at what point a given local object reference is no longer used.
ansensp 2011-03-01
  • 打赏
  • 举报
回复
不会加快, 只是在GC开始工作时, 可以多释放些内存. 是一个好习惯.
流不关, 一定会引起内存泄露. 但如果是流关闭失败, 一般就是因为流已经被关闭了, 比如网络流因为网络中断等, 这种情况, 只需处理一下异常就OK了, GC会做垃圾回收的.
v5_6000 2011-03-01
  • 打赏
  • 举报
回复
做了类似于析构函数的事情。呵呵
zqfddqr 2011-03-01
  • 打赏
  • 举报
回复
我还是认为值为null好些呵呵
王二北 2011-03-01
  • 打赏
  • 举报
回复
java中垃圾回收,对于一个对象,当没有任何引用指向该对象时,垃圾回收器就会回收该对象,释放对象所占有的内存空间
kai27ks 2011-03-01
  • 打赏
  • 举报
回复
不太懂。我觉得进入可回收序列。然后还是要再判断的吧?
加油馒头 2011-03-01
  • 打赏
  • 举报
回复
设置为null 执行垃圾回收时,会优先回收

文件关闭失败,会占用内存的,不过也会回收的
加载更多回复(8)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧