Show / Hide Table of Contents

    Class DisposableThreadLocal<T>

    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 because from the GC's standpoint the stale entries are not reclaimable.

    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 Dispose(), these hard references are cleared and then GC is freely able to reclaim space by objects stored in it.

    You should not call Dispose() until all threads are done using the instance.

    This is a Lucene.NET INTERNAL API, use at your own risk
    Inheritance
    System.Object
    DisposableThreadLocal<T>
    Namespace: Lucene.Net.Util
    Assembly: Lucene.Net.dll
    Syntax
    public class DisposableThreadLocal<T> : IDisposable
    Type Parameters
    Name Description
    T

    Methods

    | Improve this Doc View Source

    Dispose()

    Declaration
    public void Dispose()
    | Improve this Doc View Source

    Get()

    Declaration
    public virtual T Get()
    Returns
    Type Description
    T
    | Improve this Doc View Source

    InitialValue()

    Declaration
    protected virtual T InitialValue()
    Returns
    Type Description
    T
    | Improve this Doc View Source

    Set(T)

    Declaration
    public virtual void Set(T object)
    Parameters
    Type Name Description
    T object
    • Improve this Doc
    • View Source
    Back to top Copyright © 2020 Licensed to the Apache Software Foundation (ASF)