How to properly dispose of ThreadLocal variables?

M Anouti

What is the cleanest way to dispose of ThreadLocal variables so that they are subject to garbage collection? I read from the docs that:

...after a thread goes away, all of its copies of thread-local instances are subject to garbage collection (unless other references to these copies exist).

But sometimes threads can be pooled or are not expected to die. Does the ThreadLocal#remove() method actually make the value subject to garbage collection?

ben75

ThreadLocal.remove() is indeed removing a reference to the value... and if there is no more other living reference to it : the value will be soon garbage collected.

When the thread died, the thread is removed form the GC-root... therefore the entry for the thread in the ThreadLocal is subject to GC... therefore the value for this entry in the ThreadLocal is subject to GC. But once again, if you have another living ref to the value : it won't be garbage collected.

If the thread is reused (because part of a pool or ...) : it is important to call remove() so that the value can be garbage collected, but also to avoid unexpected behavior when a new job is executed on a recycled thread (the new job don't need to know the value used by the previous job)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use dispose() properly?

From Dev

How to properly dispose of a pthread mutex?

From Dev

How to properly dispose objects in Flambe?

From Dev

How to properly use the dispose method on a class

From Dev

How to dispose properly using async and await

From Dev

How to properly dispose of pinvoke/unmanaged code

From Dev

How to properly dispose the stream when using StreamContent

From Dev

How to properly use the dispose method on a class

From Dev

How to properly dispose of pinvoke/unmanaged code

From Dev

How to close or dispose of TCP client properly?

From Dev

How to properly plot variables

From Dev

How to properly dispose collection of unmanaged resources from finalizer?

From Dev

How to properly close ODP.net connection : dispose() or close()?

From Dev

How to dispose box2d shapes properly?

From Dev

How to properly dispose objects: injected vs. owned

From Dev

Memory leaks in Delphi app. How to properly dispose objects and strings?

From Dev

How to properly dispose Scene3D from QML?

From Dev

How to determine whether ContactItem in Outlook is displayed or not (dispose properly)

From Dev

Memory leaks in Delphi app. How to properly dispose objects and strings?

From Dev

How to properly dispose collection of unmanaged resources from finalizer?

From Dev

Memory leaks, and strange bugs with runnable class - How to properly dispose of objects?

From Dev

how to properly set environment variables

From Dev

How to properly use variables in PHP

From Dev

resetting variables in dispose() libgdx

From Dev

Auto Dispose Sql Connections properly

From Dev

RxSwift properly dispose subscription in closure

From Dev

How do I properly dispose and free the memory used for V8.Net.V8Engine instances?

From Dev

How to properly dispose objects created for Ldap search using ADODB ADsDSObject provider

From Dev

Using C++ classes in C: How to properly write some sort of "delete/dispose" function?

Related Related

  1. 1

    How to use dispose() properly?

  2. 2

    How to properly dispose of a pthread mutex?

  3. 3

    How to properly dispose objects in Flambe?

  4. 4

    How to properly use the dispose method on a class

  5. 5

    How to dispose properly using async and await

  6. 6

    How to properly dispose of pinvoke/unmanaged code

  7. 7

    How to properly dispose the stream when using StreamContent

  8. 8

    How to properly use the dispose method on a class

  9. 9

    How to properly dispose of pinvoke/unmanaged code

  10. 10

    How to close or dispose of TCP client properly?

  11. 11

    How to properly plot variables

  12. 12

    How to properly dispose collection of unmanaged resources from finalizer?

  13. 13

    How to properly close ODP.net connection : dispose() or close()?

  14. 14

    How to dispose box2d shapes properly?

  15. 15

    How to properly dispose objects: injected vs. owned

  16. 16

    Memory leaks in Delphi app. How to properly dispose objects and strings?

  17. 17

    How to properly dispose Scene3D from QML?

  18. 18

    How to determine whether ContactItem in Outlook is displayed or not (dispose properly)

  19. 19

    Memory leaks in Delphi app. How to properly dispose objects and strings?

  20. 20

    How to properly dispose collection of unmanaged resources from finalizer?

  21. 21

    Memory leaks, and strange bugs with runnable class - How to properly dispose of objects?

  22. 22

    how to properly set environment variables

  23. 23

    How to properly use variables in PHP

  24. 24

    resetting variables in dispose() libgdx

  25. 25

    Auto Dispose Sql Connections properly

  26. 26

    RxSwift properly dispose subscription in closure

  27. 27

    How do I properly dispose and free the memory used for V8.Net.V8Engine instances?

  28. 28

    How to properly dispose objects created for Ldap search using ADODB ADsDSObject provider

  29. 29

    Using C++ classes in C: How to properly write some sort of "delete/dispose" function?

HotTag

Archive