ThreadLocal内存泄漏原因分析

节选jdk源码中比较重要的方法进行分析,如下:

public class ThreadLocal<T> {

    private final int threadLocalHashCode = nextHashCode();
    private static AtomicInteger nextHashCode =
        new AtomicInteger();
    private static final int HASH_INCREMENT = 0x61c88647;
    private static int nextHashCode() {
        return nextHashCode.getAndAdd(HASH_INCREMENT);
    }

    public T get() {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null) {
                @SuppressWarnings("unchecked")
                T result = (T)e.value;
                return result;
            }
        }
        return setInitialValue();
    }

    public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
    }

     public void remove() {
         ThreadLocalMap m = getMap(Thread.currentThread());
         if (m != null)
             m.remove(this);
     }
    ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
    }

    static class ThreadLocalMap {
        static class Entry extends WeakReference<ThreadLocal<?>> {
            /** The value associated with this ThreadLocal. */
            Object value;
            Entry(ThreadLocal<?> k, Object v) {
                super(k);
                value = v;
            }
        }

        private static final int INITIAL_CAPACITY = 16;
        /**
         * The table, resized as necessary.
         * table.length MUST always be a power of two.
         */
        private Entry[] table;

        /**
         * The number of entries in the table.
         */
        private int size = 0;

        private int threshold; // Default to 0

        /**
         * Set the resize threshold to maintain at worst a 2/3 load factor.
         */
        private void setThreshold(int len) {
            threshold = len * 2 / 3;
        }

        private static int nextIndex(int i, int len) {
            return ((i + 1 < len) ? i + 1 : 0);
        }
        ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
            table = new Entry[INITIAL_CAPACITY];
            int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
            table[i] = new Entry(firstKey, firstValue);
            size = 1;
            setThreshold(INITIAL_CAPACITY);
        }

        private Entry getEntry(ThreadLocal<?> key) {
            int i = key.threadLocalHashCode & (table.length - 1);
            Entry e = table[i];
            if (e != null && e.get() == key)
                return e;
            else
                return getEntryAfterMiss(key, i, e);
        }
        private Entry getEntryAfterMiss(ThreadLocal<?> key, int i, Entry e) {
            Entry[] tab = table;
            int len = tab.length;

            while (e != null) {
                ThreadLocal<?> k = e.get();
                if (k == key)
                    return e;
                if (k == null)
                    expungeStaleEntry(i);
                else
                    i = nextIndex(i, len);
                e = tab[i];
            }
            return null;
        }

        private void set(ThreadLocal<?> key, Object value) {
            Entry[] tab = table;
            int len = tab.length;
            int i = key.threadLocalHashCode & (len-1);

            for (Entry e = tab[i];
                 e != null;
                 e = tab[i = nextIndex(i, len)]) {
                ThreadLocal<?> k = e.get();

                if (k == key) {
                    e.value = value;
                    return;
                }

                if (k == null) {
                    replaceStaleEntry(key, value, i);
                    return;
                }
            }

            tab[i] = new Entry(key, value);
            int sz = ++size;
            if (!cleanSomeSlots(i, sz) && sz >= threshold)
                rehash();
        }

        private void remove(ThreadLocal<?> key) {
            Entry[] tab = table;
            int len = tab.length;
            int i = key.threadLocalHashCode & (len-1);
            for (Entry e = tab[i];
                 e != null;
                 e = tab[i = nextIndex(i, len)]) {
                if (e.get() == key) {
                    e.clear();
                    expungeStaleEntry(i);
                    return;
                }
            }
        }
    }
}


可以看到,数据结构就是每个线程都有一个ThreadLocalMap类型的threadLocals变量来维护线程内的所有ThreadLocal实例。ThreadLocalMap并不继承Map,底层数据结构是一个数组ThreadLocalMap.Entry[] table数组(默认大小16),以及ThreadLocalMap.Entry(注意,它并不像HashMap那样,它并不是个链表元素,没有next引用),Entry的key是ThreadLocal对象,Entry在table中的位置由threadLocalHashCode决定,它在每次ThreadLocal初始化时被赋予值,每次都会增加 0x61c88647,注意:nextHashCode是一个静态变量.

ThreadLocal设置与获取值:

在设置值的时候,会现根据Thread.currentThread()即当前线程获取其ThreadLocalMap变量,再调用ThreadLocalMap.set方法,传入的key为ThreadLocal对象本身。那么存在哪里呢?它会根据int i = key.threadLocalHashCode & (len-1);来计算出索引,其中len为table数组的长度,接下来就对table[i]上的Entry进行判断,如果Entry的key=我们传入的key,那么就更新它。如果Entry的key为null(由于Entry的key是WeakReference,所以其key的生命周期与GC相关,下次GC时会被回收,从而导致null的出现),那么就覆盖它。否则i+1,寻找下个位置,如果找到了仍然按上述逻辑来,如果没找到,那么就会在数组尾部新建Entry并判断是否需要扩容table数组(扩容因子2/3),如果需要扩容,那么同时需要rehash操作。

在获取值的时候,会现根据Thread.currentThread()即当前线程获取其ThreadLocalMap变量,再调用ThreadLocalMap.getEntry方法,传入的key为ThreadLocal对象本身。其中会进行key.threadLocalHashCode & (table.length - 1);计算获取索引值i,如果table[i]==key,那么返回,否则就会调用getEntryAfterMiss,其内部逻辑就是,循环一直对i+1并获取索引处的Entry,如果Entry.key相等返回,如果Entry,key为null,清除对应的值(为防止内存泄漏的一个举措).

还有个remove方法:

其会清除Entry的key及对应的value

ThreadLocal如何保证隔离各个线程呢?

前面说了,ThreadLocal的set/get底层都是通过ThreadLocalMap来进行的,而每个线程都有自己的ThreadLocalMap变量,通过Thread.currentThread().threadLocals来获取。所以这样就确保了每个线程的ThreadLocal对其他线程不可见。那么我在一个线程初始化的时候拿到了另一个线程的引用,比如在main thread new 一个 thread,那么main thread就获取了那个thread的引用t,此时,我通过t.threadLocals来获取这个ThreadLocalMap并操作其中的ThreadLocal行不行?亲爱的,这是不行的。因threadLocals是默认的访问修饰,也就是说只有当前包(java.lang)情况下可访问.

那为什么ThreadLocal变量会导致内存泄漏呢?

首先来回顾下Entry的代码

static class Entry extends WeakReference<ThreadLocal<?>> {
            /** The value associated with this ThreadLocal. */
            Object value;
            Entry(ThreadLocal<?> k, Object v) {
                super(k);
                value = v;
            }
        }


Entry是实现了弱引用的,Java中有四种引用类型,弱引用呢,就是每次GC时都会被回收的。但要注意的是,此处的弱引用针对的是key,而value仍然是强引用。

从前面的代码我们看到,set方法在碰到Entry.key==null是时会调用replaceStaleEntry,而replaceStaleEntry内部又会调用expungeStaleEntry, get方法则在碰到Entry.key==null时直接调用expungeStaleEntry。那么我们来看看这个expungeStaleEntry代码:

private int expungeStaleEntry(int staleSlot) {
            Entry[] tab = table;
            int len = tab.length;

            // expunge entry at staleSlot
            tab[staleSlot].value = null;
            tab[staleSlot] = null;
            size--;

            // Rehash until we encounter null
            Entry e;
            int i;
            for (i = nextIndex(staleSlot, len);
                 (e = tab[i]) != null;
                 i = nextIndex(i, len)) {
                ThreadLocal<?> k = e.get();
                if (k == null) {
                    e.value = null;
                    tab[i] = null;
                    size--;
                } else {
                    int h = k.threadLocalHashCode & (len - 1);
                    if (h != i) {
                        tab[i] = null;

                        // Unlike Knuth 6.4 Algorithm R, we must scan until
                        // null because multiple entries could have been stale.
                        while (tab[h] != null)
                            h = nextIndex(h, len);
                        tab[h] = e;
                    }
                }
            }
            return i;
        }


可以看到它除了释放索引i处Entry的key,value引用之外,还会遍历i后面的索引,只要碰到Entry.key为null的都会进行释放。同时会对已有不在hash定位处的Entry进行移动位置,以降低后续哈希碰撞的几率。

一言以蔽之,就是ThreadLocal本身为防止内存泄漏作出了一定的努力,首先Entry.key为弱引用,下次GC ThreadLocal实例会被回收,但是Entry.value是强引用,需要在当前线程的任意一个get,set调用并且碰到Entry.key==null的情形下会清除对于的Entry并释放value引用。

那么问题来了,当我们使用线程池的时候,万一这该死的线程一直处理存活状态(不断运行不同的Runnable,每个Runnable又new一个或多个ThreadLocal),而且get,set大部分时候都没碰到Entry.key==null的情形(threadLocalHashCode & (len-1)说怪我咯),那么就会导致内存泄漏。其实出现这样的几率有点低,对吧?但是毕竟是存在这样的可能性嘛,那么如何防范呢?其实只要我们养成一个好习惯就可以了,那就是每次使用完ThreadLocal后,调用其remove方法即可防止内存泄漏。

评论区
Rick ©2018