<!-- 
RSS generated by JIRA (4.4#649-r158309) at Wed Jun 19 21:40:57 CDT 2013

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary add field=key&field=summary to the URL of your request.
For example:
http://dev.clojure.org/jira/si/jira.issueviews:issue-xml/CLJ-1152/CLJ-1152.xml?field=key&field=summary
-->
<rss version="0.92" >
<channel>
    <title>Clojure JIRA</title>
    <link>http://dev.clojure.org/jira</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>4.4</version>
        <build-number>649</build-number>
        <build-date>25-07-2011</build-date>
    </build-info>

<item>
            <title>[CLJ-1152] PermGen leak in multimethods and protocol fns</title>
                <link>http://dev.clojure.org/jira/browse/CLJ-1152</link>
                <project id="10010" key="CLJ">Clojure</project>
                        <description>&lt;p&gt;There is a PermGen memory leak that we have tracked down to protocol methods and multimethods called inside an &lt;tt&gt;eval&lt;/tt&gt;, because of the caches these methods use. The problem only arises when the value being cached is an instance of a class (such as a function or reify) that was defined inside the &lt;tt&gt;eval&lt;/tt&gt;. Thus extending &lt;tt&gt;IFn&lt;/tt&gt; or dispatching a multimethod on an &lt;tt&gt;IFn&lt;/tt&gt; are likely triggers.&lt;/p&gt;

&lt;p&gt;My fellow LonoClouder, &lt;b&gt;Jeff Dik&lt;/b&gt; describes how to reproduce and work around the problem:&lt;/p&gt;

&lt;p&gt;The easiest way that I have found to test this is to set &quot;&lt;tt&gt;-XX:MaxPermSize&lt;/tt&gt;&quot; to a reasonable value so you don&apos;t have to wait too long for the PermGen space to fill up, and to use &quot;&lt;tt&gt;-XX:+TraceClassLoading&lt;/tt&gt;&quot; and &quot;&lt;tt&gt;-XX:+TraceClassUnloading&lt;/tt&gt;&quot; to see the classes being loaded and unloaded.&lt;/p&gt;

&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeHeader panelHeader&quot; style=&quot;border-bottom-width: 1px;&quot;&gt;&lt;b&gt;leiningen project.clj&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;(defproject permgen-scratch &lt;span class=&quot;code-quote&quot;&gt;&quot;0.1.0-SNAPSHOT&quot;&lt;/span&gt;
  :dependencies [[org.clojure/clojure &lt;span class=&quot;code-quote&quot;&gt;&quot;1.5.0-RC1&quot;&lt;/span&gt;]]
  :jvm-opts [&lt;span class=&quot;code-quote&quot;&gt;&quot;-XX:MaxPermSize=32M&quot;&lt;/span&gt;
             &lt;span class=&quot;code-quote&quot;&gt;&quot;-XX:+TraceClassLoading&quot;&lt;/span&gt;
             &lt;span class=&quot;code-quote&quot;&gt;&quot;-XX:+TraceClassUnloading&quot;&lt;/span&gt;])&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can use &lt;tt&gt;lein swank 45678&lt;/tt&gt; and connect with slime in emacs via &lt;tt&gt;M-x slime-connect&lt;/tt&gt;.&lt;/p&gt;

&lt;p&gt;To monitor the PermGen usage, you can find the Java process to watch with &quot;&lt;tt&gt;jps -lmvV&lt;/tt&gt;&quot; and then run &quot;&lt;tt&gt;jstat -gcold &lt;ins&gt;&lt;em&gt;&amp;lt;PROCESS_ID&amp;gt;&lt;/em&gt;&lt;/ins&gt; 1s&lt;/tt&gt;&quot;. According to &lt;a href=&quot;http://docs.oracle.com/javase/7/docs/technotes/tools/share/jstat.html#gcold_option&quot;&gt;the jstat docs&lt;/a&gt;, the first column (PC) is the &quot;Current permanent space capacity (KB)&quot; and the second column (PU) is the &quot;Permanent space utilization (KB)&quot;. VisualVM is also a nice tool for monitoring this.&lt;/p&gt;

&lt;h2&gt;&lt;a name=&quot;Multimethodleak&quot;&gt;&lt;/a&gt;Multimethod leak&lt;/h2&gt;

&lt;p&gt;Evaluating the following code will run a loop that eval&apos;s &lt;tt&gt;(take* (fn foo []))&lt;/tt&gt;.&lt;/p&gt;

&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeHeader panelHeader&quot; style=&quot;border-bottom-width: 1px;&quot;&gt;&lt;b&gt;multimethod leak&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;(defmulti take* (fn [a] (type a)))

(defmethod take* clojure.lang.Fn
  [a]
  &apos;())

(def stop (atom &lt;span class=&quot;code-keyword&quot;&gt;false&lt;/span&gt;))
(def sleep-duration (atom 1000))

(defn run-loop []
  (when-not @stop
    (eval &apos;(take* (fn foo [])))
    (&lt;span class=&quot;code-object&quot;&gt;Thread&lt;/span&gt;/sleep @sleep-duration)
    (recur)))

(&lt;span class=&quot;code-keyword&quot;&gt;future&lt;/span&gt; (run-loop))

(reset! sleep-duration 0)&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the &lt;tt&gt;lein swank&lt;/tt&gt; session, you will see many lines like below listing the classes being created and loaded.&lt;/p&gt;

&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;[Loaded user$eval15802$foo__15803 from __JVM_DefineClass__]
[Loaded user$eval15802 from __JVM_DefineClass__]&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;These lines will stop once the PermGen space fills up.&lt;/p&gt;

&lt;p&gt;In the jstat monitoring, you&apos;ll see the amount of used PermGen space (PU) increase to the max and stay there.&lt;/p&gt;

&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;-    PC       PU        OC          OU       YGC    FGC    FGCT     GCT
 31616.0  31552.7    365952.0         0.0      4     0    0.000    0.129
 32000.0  31914.0    365952.0         0.0      4     0    0.000    0.129
 32768.0  32635.5    365952.0         0.0      4     0    0.000    0.129
 32768.0  32767.6    365952.0      1872.0      5     1    0.000    0.177
 32768.0  32108.2    291008.0     23681.8      6     2    0.827    1.006
 32768.0  32470.4    291008.0     23681.8      6     2    0.827    1.006
 32768.0  32767.2    698880.0     24013.8      8     4    1.073    1.258
 32768.0  32767.2    698880.0     24013.8      8     4    1.073    1.258
 32768.0  32767.2    698880.0     24013.8      8     4    1.073    1.258&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A workaround is to run &lt;tt&gt;prefer-method&lt;/tt&gt; before the PermGen space is all used up, e.g.&lt;/p&gt;
&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;(prefer-method take* clojure.lang.Fn java.lang.&lt;span class=&quot;code-object&quot;&gt;Object&lt;/span&gt;)&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then, when the used PermGen space is close to the max, in the &lt;tt&gt;lein swank&lt;/tt&gt; session, you will see the classes created by the eval&apos;ing being unloaded.&lt;/p&gt;
&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;[Unloading class user$eval5950$foo__5951]
[Unloading class user$eval3814]
[Unloading class user$eval2902$foo__2903]
[Unloading class user$eval13414]&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the jstat monitoring, there will be a long pause when used PermGen space stays close to the max, and then it will drop down, and start increasing again when more eval&apos;ing occurs.&lt;/p&gt;

&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;-    PC       PU        OC          OU       YGC    FGC    FGCT     GCT
 32768.0  32767.9    159680.0     24573.4      6     2    0.167    0.391
 32768.0  32767.9    159680.0     24573.4      6     2    0.167    0.391
 32768.0  17891.3    283776.0     17243.9      6     2   50.589   50.813
 32768.0  18254.2    283776.0     17243.9      6     2   50.589   50.813&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;tt&gt;defmulti&lt;/tt&gt; defines a cache that uses the dispatch values as keys. Each eval call in the loop defines a new foo class which is then added to the cache when &lt;tt&gt;take*&lt;/tt&gt; is called, preventing the class from ever being GCed.&lt;/p&gt;

&lt;p&gt;The prefer-method workaround works because it calls &lt;tt&gt;clojure.lang.MultiFn.preferMethod&lt;/tt&gt;, which calls the private &lt;tt&gt;MultiFn.resetCache&lt;/tt&gt; method, which completely empties the cache.&lt;/p&gt;

&lt;h2&gt;&lt;a name=&quot;Protocolleak&quot;&gt;&lt;/a&gt;Protocol leak&lt;/h2&gt;

&lt;p&gt;The leak with protocol methods similarly involves a cache. You see essentially the same behavior as the multimethod leak if you run the following code using protocols.&lt;/p&gt;

&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeHeader panelHeader&quot; style=&quot;border-bottom-width: 1px;&quot;&gt;&lt;b&gt;protocol leak&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;(defprotocol ITake (take* [a]))

(extend-type clojure.lang.Fn
  ITake
  (take* [&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;] &apos;()))

(def stop (atom &lt;span class=&quot;code-keyword&quot;&gt;false&lt;/span&gt;))
(def sleep-duration (atom 1000))

(defn run-loop []
  (when-not @stop
    (eval &apos;(take* (fn foo [])))
    (&lt;span class=&quot;code-object&quot;&gt;Thread&lt;/span&gt;/sleep @sleep-duration)
    (recur)))

(&lt;span class=&quot;code-keyword&quot;&gt;future&lt;/span&gt; (run-loop))

(reset! sleep-duration 0)&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Again, the cache is in the &lt;tt&gt;take*&lt;/tt&gt; method itself, using each new &lt;tt&gt;foo&lt;/tt&gt; class as a key.&lt;/p&gt;

&lt;p&gt;A workaround is to run &lt;tt&gt;-reset-methods&lt;/tt&gt; on the protocol before the PermGen space is all used up, e.g.&lt;/p&gt;
&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;(-reset-methods ITake)&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This works because &lt;tt&gt;-reset-methods&lt;/tt&gt; replaces the cache with an empty MethodImplCache.&lt;/p&gt;</description>
                <environment></environment>
            <key id="15981">CLJ-1152</key>
            <summary>PermGen leak in multimethods and protocol fns</summary>
                <type id="1" iconUrl="http://dev.clojure.org/jira/images/icons/bug.gif">Defect</type>
                                <priority id="4" iconUrl="http://dev.clojure.org/jira/images/icons/priority_minor.gif">Minor</priority>
                    <status id="1" iconUrl="http://dev.clojure.org/jira/images/icons/status_open.gif">Open</status>
                    <resolution id="-1">Unresolved</resolution>
                                <assignee username="-1">Unassigned</assignee>
                                <reporter username="chouser@n01se.net">Chouser</reporter>
                        <labels>
                    </labels>
                <created>Wed, 30 Jan 2013 09:00:47 -0600</created>
                <updated>Wed, 30 Jan 2013 09:10:08 -0600</updated>
                                    <version>Release 1.4</version>
                                                        <due></due>
                    <votes>1</votes>
                        <watches>0</watches>
                        <comments>
                    <comment id="30511" author="chouser@n01se.net" created="Wed, 30 Jan 2013 09:10:08 -0600"  >&lt;p&gt;I think the most obvious solution would be to constrain the size of the cache.  Adding an item to the cache is already not the fastest path, so a bit more work could be done to prevent the cache from growing indefinitely large. &lt;/p&gt;

&lt;p&gt;That does raise the question of what criteria to use.  Keep the first &lt;em&gt;n&lt;/em&gt; entries?  Keep the &lt;em&gt;n&lt;/em&gt; most recently used (which would require bookkeeping in the fast cache-hit path)? Keep the &lt;em&gt;n&lt;/em&gt; most recently added?&lt;/p&gt;</comment>
                </comments>
                    <attachments>
                </attachments>
            <subtasks>
        </subtasks>
                <customfields>
                                                                                            <customfield id="customfield_10010" key="com.pyxis.greenhopper.jira:gh-global-rank">
                <customfieldname>Global Rank</customfieldname>
                <customfieldvalues>
                    
                </customfieldvalues>
            </customfield>
                                                                                                            </customfields>
    </item>
</channel>
</rss>