首頁 >Java >java教程 >Java中ThreadLocal導致記憶體溢出(Out Of Memory)的原因是什麼?

Java中ThreadLocal導致記憶體溢出(Out Of Memory)的原因是什麼?

WBOY
WBOY轉載
2023-04-23 23:58:091108瀏覽

原因分析

ThreadLocal 導致記憶體 OOM 的原因是什麼?

ThreadLocal 底層透過ThreadLocalMap 儲存資料

#原始碼如下: 

Java中ThreadLocal導致記憶體溢出(Out Of Memory)的原因是什麼?

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

原始碼如下:

Java中ThreadLocal導致記憶體溢出(Out Of Memory)的原因是什麼?

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

原始碼如下: 

Java中ThreadLocal導致記憶體溢出(Out Of Memory)的原因是什麼?

#雖然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 Thread 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導致記憶體溢出(Out Of Memory)的原因是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除