简体   繁体   English

为什么我有 memory 泄漏与通道 sub/unsub 以下代码?

[英]Why do I have memory leak for the following code with channel sub/unsub?

I am using [org.clojure/clojure "1.10.1"],[org.clojure/core.async "1.2.603"] and the latest Amazon Corretto 11 JVM if there was anything to do with them.我正在使用[org.clojure/clojure "1.10.1"],[org.clojure/core.async "1.2.603"]和最新的 Amazon Corretto 11 JVM,如果它们有任何关系的话。

The following code is a simplified version of the code used in production and it does cause memory leak.以下代码是生产中使用的代码的简化版本,它确实会导致 memory 泄漏。 I have no idea why that happened but suspect it might due to sub/unsub of channels.我不知道为什么会发生这种情况,但怀疑这可能是由于频道的子/取消子。 Can anyone help point out where my code may go wrong or how I can fix the memory leak?谁能帮助指出我的代码可能在哪里 go 错误或我如何修复 memory 泄漏?

(ns test-gc.core
  (:require [clojure.core.async :as a :refer [chan put! close! <! go >! go-loop timeout]])
  (:import [java.util UUID]))

(def global-msg-ch (chan (a/sliding-buffer 200)))

(def global-msg-pub (a/pub global-msg-ch :id))

(defn io-promise []
  (let [id (UUID/randomUUID)
        ch (chan)]
    (a/sub global-msg-pub id ch)
    [id (go
          (let [x (<! ch)]
            (a/unsub global-msg-pub id ch)
            (:data x)))]))

(defn -main []
  (go-loop []
    (<! (timeout 1))
    (let [[pid pch] (io-promise)
          cmd {:id   pid
               :data (rand-int 1E5)}]
      (>! global-msg-ch cmd)
      (println (<! pch)))
    (recur))
  (while true
    (Thread/yield)))

A quick heap dump gives the following statistics for example:例如,快速堆转储提供以下统计信息:

  • Class by number of instances Class 按实例数

    • java.util.LinkedList 5,157,128 (14.4%) java.util.LinkedList 5,157,128 (14.4%)
    • java.util.concurrent.atomic.AtomicReference 3,698,382 (10.3%) java.util.concurrent.atomic.AtomicReference 3,698,382 (10.3%)
    • clojure.lang.Atom 3,094,279 (8.6%) clojure.lang.Atom 3,094,279 (8.6%)
    • ... ...
  • Class by size of instances Class 按实例大小

    • java.lang.Object[] 210,061,752 B (13.8%) java.lang.Object[] 210,061,752 B (13.8%)
    • java.util.LinkedList 206,285,120 B (13.6%) java.util.LinkedList 206,285,120 B (13.6%)
    • clojure.lang.Atom 148,525,392 B (9.8%) clojure.lang.Atom 148,525,392 B (9.8%)
    • clojure.core.async.impl.channels.ManyToManyChannel 132,022,336 B (8.7%) clojure.core.async.impl.channels.ManyToManyChannel 132,022,336 B (8.7%)
    • ... ...

I finally figured out why.我终于明白为什么了。 By looking at the source code, we get the following segment:通过查看源代码,我们得到以下部分:

(defn pub
  "Creates and returns a pub(lication) of the supplied channel, ..."
  ...
     (let [mults (atom {}) ;;topic->mult
           ensure-mult (fn [topic]
                         (or (get @mults topic)
                             (get (swap! mults
                                         #(if (% topic) % (assoc % topic (mult (chan (buf-fn topic))))))
                                  topic)))
           p (reify
              Mux
              (muxch* [_] ch)

              Pub
              (sub* [p topic ch close?]
                    (let [m (ensure-mult topic)]
                      (tap m ch close?)))
              (unsub* [p topic ch]
                      (when-let [m (get @mults topic)]
                        (untap m ch)))
              (unsub-all* [_] (reset! mults {}))
              (unsub-all* [_ topic] (swap! mults dissoc topic)))]
       ...
       p)))

We can see mults stores all topic hence shall increase monotonically if we do not clear it.我们可以看到mults存储所有topic ,因此如果我们不清除它,它将单调增加。 We may add something like (a/unsub-all* global-msg-pub pid) to fix that.我们可以添加类似(a/unsub-all* global-msg-pub pid)的东西来解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM