Reputation: 418
If you have a thread and the thread is causing some possible memory leaks, If you kill the thread, will all the memory will be released? I am trying to understand how does the clean up works?
Upvotes: 2
Views: 298
Reputation: 1062770
If you resort to killing a thread in the Thread.Abort()
sense, you're pretty much saying "this process is so ill that I'm about to put it out of its misery", in which case it really doesn't matter what the memory semantics are. Killing a thread is a very bad thing, and can violate a large number of things we think about in terms of how code behaves.
But: in terms of how memory behaves; threads (and specifically: the active portion of the stack, and any thread-static values / thread slots) are one of many kinds of "root". Killing a thread will logically remove one root, so the next time GC runs, if something was only alive because it was directly or indirectly reachable from the stack-frame / thread-statics / thread-slots of that thread, then it will be eligible for collection. Of course, killing a thread in this way may actually increase problems, especially for unmanaged code - since it can stop using
/ Dispose()
code working correctly, meaning that more finalizers may need to run. The stack space itself may also be recoverable, but in the grand scheme of things, that tends to me negligible - and the process may wish to keep hold of it on the basis that you'll probably want another thread.
Again: emphasis, do not kill threads. If you can set a signal somewhere that they check periodically and unwind themselves, then: fine.
Upvotes: 1