Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
Public Member Functions | Protected Member Functions | List of all members
Lucene.Net.Util.CloseableThreadLocal< T > Class Template Reference

Java's builtin ThreadLocal has a serious flaw: it can take an arbitrarily long amount of time to dereference the things you had stored in it, even once the ThreadLocal instance itself is no longer referenced. This is because there is single, master map stored for each thread, which all ThreadLocals share, and that master map only periodically purges "stale" entries. More...

Inherits IDisposable.

Public Member Functions

virtual T InitialValue ()
 
virtual T Get ()
 
virtual void Set (T @object)
 
virtual void Close ()
 
void Dispose ()
 

Protected Member Functions

virtual void Dispose (bool disposing)
 

Detailed Description

Java's builtin ThreadLocal has a serious flaw: it can take an arbitrarily long amount of time to dereference the things you had stored in it, even once the ThreadLocal instance itself is no longer referenced. This is because there is single, master map stored for each thread, which all ThreadLocals share, and that master map only periodically purges "stale" entries.

While not technically a memory leak, because eventually the memory will be reclaimed, it can take a long time and you can easily hit OutOfMemoryError because from the GC's standpoint the stale entries are not reclaimaible.

This class works around that, by only enrolling WeakReference values into the ThreadLocal, and separately holding a hard reference to each stored value. When you call Close, these hard references are cleared and then GC is freely able to reclaim space by objects stored in it.

Type Constraints
T :class 

Definition at line 53 of file CloseableThreadLocal.cs.

Member Function Documentation

virtual void Lucene.Net.Util.CloseableThreadLocal< T >.Close ( )
virtual

Definition at line 117 of file CloseableThreadLocal.cs.

void Lucene.Net.Util.CloseableThreadLocal< T >.Dispose ( )

Definition at line 122 of file CloseableThreadLocal.cs.

virtual void Lucene.Net.Util.CloseableThreadLocal< T >.Dispose ( bool  disposing)
protectedvirtual

Definition at line 127 of file CloseableThreadLocal.cs.

virtual T Lucene.Net.Util.CloseableThreadLocal< T >.Get ( )
virtual

Definition at line 67 of file CloseableThreadLocal.cs.

virtual T Lucene.Net.Util.CloseableThreadLocal< T >.InitialValue ( )
virtual

Definition at line 62 of file CloseableThreadLocal.cs.

virtual void Lucene.Net.Util.CloseableThreadLocal< T >.Set ( T @  object)
virtual

Definition at line 87 of file CloseableThreadLocal.cs.


The documentation for this class was generated from the following file: