死线程是否会导致内存泄漏?
死线程会对内存使用造成威胁吗?最终会被GC收集吗?
Is a dead thread a threat to memory usage? Will it eventually get collected by the GC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
死线程会对内存使用造成威胁吗?最终会被GC收集吗?
Is a dead thread a threat to memory usage? Will it eventually get collected by the GC?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
与任何其他对象一样,如果无法通过对死线程的任何引用来访问死线程,GC 将收集该死线程。将指向它的 ALL 变量设置为
null
应该可以使其可供 GC 收集。对内存使用的威胁……这取决于您使用了多少内存——以及对其他对象实例(列表、哈希表等)的引用数量。它对内存使用的威胁与任何其他类型的对象完全相同。
A dead thread, as any other object, will be collected by the GC if cannot be reached by any references to it. Setting ALL variables that point to it to
null
should make it available for collection by the GC.A threat to memory usage ... it depends on how much memory you're using in it - and how many references to other object instances (Lists, Hashtables, ...). It's exactly the same threat to memory usage as any other type of object.
显然,如果你的代码仍然有一个引用,那么线程对象仍然存在,你无法重新启动它,但它仍然是一个可用的java对象(抱歉,如果这太明显了,最好先消除它;-))
同样的讨论也发生了在这里。
http://www.coderanch.com/t /531074/threads/java/dead-thread-threat-memory-leak
Obviously if your code still has a reference the thread object is still there, you can't restart it but its still a usable java object (sorry if that was too obvious, best to eliminate that first ;-) )
The same discussion has happened over here .
http://www.coderanch.com/t/531074/threads/java/dead-thread-threat-memory-leak
将线程对象设置为 null 应保证对死线程对象进行 GC。
Setting the thread object to null should guarantee GC on the dead thread object.
只需将不再使用的所有资源放置在
null
中就可以了,这样比较安全,然后抱歉。 GC 会捡起它。Just put all resources you don't use anymore to rest by it putting it to
null
better safe then sorry. GC will pick that up.