开发者

Is a dead thread a threat to memory leak?

开发者 https://www.devze.com 2023-02-22 16:16 出处:网络
Is a dead th开发者_如何学Goread a threat to memory usage? Will it eventually get collected by the GC?A dead thread, as any other object, will be collected by the GC if cannot be reached by any referen

Is a dead th开发者_如何学Goread a threat to memory usage? Will it eventually get collected by the 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.


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


Setting the thread object to null should guarantee GC on the dead thread object.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消