Java中ThreadLocal 導致內存 OOM 的原因分析

原因分析

ThreadLocal 導致內存 OOM 的原因是什麼?

ThreadLocal 底層通過 ThreadLocalMap 存儲數據

源碼如下: 

  • 當我們使用ThreadLocal.set()時,set的value與key(即業務自己定義的ThreadLocal類)會存儲在ThreadLocalMap的Entry[]數組裡

源碼如下:

  • 其中Entry是實現瞭一個弱引用WeakReference,Entry的key(即業務方定義的 ThreadLocal類)會被包裝成一個弱引用當成Entry的key。Java的弱引用的定義是,當JVM執行垃圾回收掃描的時候,當發現隻有弱引用的對象時,會立即回收此對象,這是ThreadLocal當初設計的時候防止內存溢出的一個手段

源碼如下: 

雖然key被包裝成瞭一個弱引用會被垃圾回收機制給回收,但是value在線程(Thread)不死亡時卻可能存在一條強引用鏈.

由於 value是強引用,隻要 Thread不死亡時,例如線程池,這條強引用鏈就會存在,那麼value就不會回收,可能造成內存溢出

引用關系如下: Thread ==> ThreadLocalMap ==> Entry ==> value

但是這個消除強引用鏈的動作是需要業務方在get的情況下觸發的,可能業務方並不會get、也可能get是key不為空,並不會觸發 expungeStaleEntry 類。所以開發者要養成良好的習慣,記得用完 ThreadLocal 時,調一次ThreadLocal.remove()方法或者 ThreadLocal.set(null)

正確的使用方式

public class ThreadLocalTest {
    /**
     * 未初始化的本地線程變量
     */
    private static ThreadLocal<User> userThreadLocal = new ThreadLocal<>();
    public static void main(String[] args) throws InterruptedException {
        int threadNum = 10;
        List<Thread> threadList = Lists.newArrayList();

        for (int i = 0; i < threadNum; ++i) {
            long userId = i;
            Thread t = new Thread(() -> {
                try {
                    // 設置變量值
                    userThreadLocal.set(new User(userId, "lanxing" + userId, "2x"));
                    // 使用變量
                    doSomething();
                } finally {
                    // 移除變量
                    userThreadLocal.remove();   //移除ThreadLocal變量
                }
            }, "T" + i);
            threadList.add(t);
            t.start();
        }

        for (int i = 0; i < threadNum; ++i) {
            threadList.get(i).join();
        }
    }
    private static void doSomething() {
        log.info("Use ThreadLocal variable :{}", JSON.toJSONString(userThreadLocal.get()));
    }
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class User {
    private Long id;
    private String name;
    private String level;
}

打印結果:

14:30:26.790 [T2] INFO io.zhengsh.order.tool.ThreadLocalTest – Use ThreadLocal variable :{"id":2,"level":"2x","name":"lanxing2"}
14:30:26.789 [T5] INFO io.zhengsh.order.tool.ThreadLocalTest – Use ThreadLocal variable :{"id":5,"level":"2x","name":"lanxing5"}
14:30:26.792 [T0] INFO io.zhengsh.order.tool.ThreadLocalTest – Use ThreadLocal variable :{"id":0,"level":"2x","name":"lanxing0"}
14:30:26.792 [T4] INFO io.zhengsh.order.tool.ThreadLocalTest – Use ThreadLocal variable :{"id":4,"level":"2x","name":"lanxing4"}
14:30:26.792 [T8] INFO io.zhengsh.order.tool.ThreadLocalTest – Use ThreadLocal variable :{"id":8,"level":"2x","name":"lanxing8"}
14:30:26.791 [T1] INFO io.zhengsh.order.tool.ThreadLocalTest – Use ThreadLocal variable :{"id":1,"level":"2x","name":"lanxing1"}
14:30:26.792 [T7] INFO io.zhengsh.order.tool.ThreadLocalTest – Use ThreadLocal variable :{"id":7,"level":"2x","name":"lanxing7"}
14:30:26.792 [T6] INFO io.zhengsh.order.tool.ThreadLocalTest – Use ThreadLocal variable :{"id":6,"level":"2x","name":"lanxing6"}
14:30:26.791 [T9] INFO io.zhengsh.order.tool.ThreadLocalTest – Use ThreadLocal variable :{"id":9,"level":"2x","name":"lanxing9"}
14:30:26.790 [T3] INFO io.zhengsh.order.tool.ThreadLocalTest – Use ThreadLocal variable :{"id":3,"level":"2x","name":"lanxing3"}

到此這篇關於Java中ThreadLocal 導致內存 OOM 的原因分析的文章就介紹到這瞭,更多相關Java 內存OOM 內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: