java ThreadLocal 简介以及内存泄露分析

ThreadLocal简介:

ThreadLocal可以实现线程私有的变量存储。将变量和线程绑定,不同于局部变量,ThreadLocal中存储的值在方法退出之后,依然存在,只要根据当前线程获取到Thread类,就可以获取到ThreadLocal中存储的数据。将变量线程私有化,可以减少并发问题,减少锁的开销,提高效率。

1. ThreadLocal类主要操作Thread类里面的ThreadLocalMap对象,来实现线程私有变量的读写。

下面是ThreadLocal的get和set, 可以看到get和set的时候其实是调用了getMap方法来获取Thread类里面的ThreadLocalMap对象,从而操作ThreadLocalMap中的Entry来实现对象的存储的。

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);
    }

ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
    }

2. Thread类里面持有一个ThreadLocalMap对象,ThreadLocalMap里面有个Entry数组,Entry数组的key是ThreadLocal对象, value是用户要存放的线程私有对象。代码结构入下图

 

3. 数组下标根据Hash算法得出,对于hash碰撞的处理,采用了线性探测法,set数据的时候,如果数组在当前下标下已有数据,并且还不是自己的话,则下标+1,再去判断是否可以存放。直到存放下为止。

ThreadLocal与内存泄露

ThreadLocal与Thread类在堆栈内存中的引用关系,如图所示:

ThreadLocal在使用的时候容易引发内存泄露,因为Thread类中的ThreadLocalMap的key是ThreadLocal对象。如果该ThreadLocal对象不在使用了之后,map中还存在对该ThreadLocal对象的引用,使其无法被垃圾回收。ThreadLocalMap的解决办法是将ThreadLocalMap中的key对ThreadLocal的引用,使用的是WeekReference 弱引用, 这样当该ThreadLocal对象不使用后,只存在一个弱引用,不影响其可以被GC回收。对于key解决了内存泄露问题。但是value是没办法回收的。所以每次在调用ThreadLocal的set的时候,会扫描所有key为null的元素,将其value=null。 这样value可以在下次GC的时候回收。

我们在使用的时候,如果某个ThreadLocal不用了的话,虽然有清理机制,但还是尽量手动删除,因为清理操作需要set方法调用的时候才去触发,如果不调用set,就会引起内存泄露。

下面是ThreadLocal在set元素的时候,调用cleanSomeSlots() 方法删除无效元素的过程。

        /**
         * Set the value associated with key.
         *
         * @param key the thread local object
         * @param value the value to be set
         */
        private void set(ThreadLocal<?> key, Object value) {

            // We don't use a fast path as with get() because it is at
            // least as common to use set() to create new entries as
            // it is to replace existing ones, in which case, a fast
            // path would fail more often than not.

            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();
        }


         /**
         * Heuristically scan some cells looking for stale entries.
         * This is invoked when either a new element is added, or
         * another stale one has been expunged. It performs a
         * logarithmic number of scans, as a balance between no
         * scanning (fast but retains garbage) and a number of scans
         * proportional to number of elements, that would find all
         * garbage but would cause some insertions to take O(n) time.
         *
         * @param i a position known NOT to hold a stale entry. The
         * scan starts at the element after i.
         *
         * @param n scan control: {@code log2(n)} cells are scanned,
         * unless a stale entry is found, in which case
         * {@code log2(table.length)-1} additional cells are scanned.
         * When called from insertions, this parameter is the number
         * of elements, but when from replaceStaleEntry, it is the
         * table length. (Note: all this could be changed to be either
         * more or less aggressive by weighting n instead of just
         * using straight log n. But this version is simple, fast, and
         * seems to work well.)
         *
         * @return true if any stale entries have been removed.
         */
        private boolean cleanSomeSlots(int i, int n) {
            boolean removed = false;
            Entry[] tab = table;
            int len = tab.length;
            do {
                i = nextIndex(i, len);
                Entry e = tab[i];
                if (e != null && e.get() == null) {
                    n = len;
                    removed = true;
                    i = expungeStaleEntry(i);
                }
            } while ( (n >>>= 1) != 0);
            return removed;
        }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值