Skip to content

Instantly share code, notes, and snippets.

@jsmorph
Last active December 25, 2015 21:19
Show Gist options
  • Save jsmorph/7042092 to your computer and use it in GitHub Desktop.
Save jsmorph/7042092 to your computer and use it in GitHub Desktop.
Ugly mitigation of CLJ-1152 (PermGen) bugs in the protocols case
;; Ugly mitigation of http://dev.clojure.org/jira/browse/CLJ-1152 in
;; the protocols case. Motivation: Need to eval lots domain-specific
;; language code rendered in core.logic. Approach: Start a thread to
;; purge protocol caches every second. You should probable write a
;; fancier version.
(defn protocol? [x]
(and (instance? clojure.lang.PersistentArrayMap x)
(boolean (:on-interface x))))
(defn protocols
"Get a seq of protocols in the given namespace."
([namespace]
(binding [*ns* (if (symbol? namespace)
(find-ns namespace)
namespace)]
(doall (filter protocol?
(map var-get
(filter var?
(map second
(ns-map *ns*))))))))
([]
(protocols *ns*)))
(defonce simple-protocol-cache-cleaner
;; Starts a simple daemon thread that purges the caches for all protocols in the current namespace.
(let [interval 1000 ;; ms
ps (protocols *ns*)]
(doto (new Thread (fn []
(loop [i 0]
(doseq [p ps] (-reset-methods p))
(Thread/sleep interval)
(recur (inc i)))))
(.setDaemon true)
(.start))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment