<div dir="ltr"><div><div><div><div><div><div><div><div><div><div>Hi Bob,<br></div><div>yes, you force me to rethink. The way mark and sweep GC works, cycles in object graphs do not matter.<br></div><div>As long as the objects in the cycles are only pointed to weakly from roots, <br></div><br></div><div>| value weak array2 |<br></div>value := Array new.<br></div>weak := WeakArray with: value.<br></div>3 timesRepeat: [Smalltalk garbageCollect].<br></div>self assert: (weak count: #notNil) = 1. "Not yet reclaimed, thisContext points to it thru value variable"
<div><div>value := nil.<br></div>3 timesRepeat: [Smalltalk garbageCollect].<br></div>self assert: (weak count: #notNil) = 0. "This time, it is reclaimed"

<br><br></div><div>"Example of cycle"<br></div><div>value := Array new: 1.<br></div><div>array2 := Array with: value.<br></div><div><div><div>value at: 1 put: array2.<br></div>weak := WeakArray with: value.<br></div>array2 := nil.<br></div><div><div>value := nil.<br></div>3 timesRepeat: [Smalltalk garbageCollect].<br></div>self assert: (weak count: #notNil) = 0. "The value is reclaimed despite the cycle"



<br><br></div>It's then VW that behaves strangely: I did not find any pointer to the class nor its metaclass apart themselves<br></div>(I took care to not add the metaclass to metaclass superclass subclasses, because those are not weak).<br></div>This has obscured my mind.<br></div>Sorry for the noise.<br></div><div class="gmail_extra"><br><div class="gmail_quote">2018-03-26 23:15 GMT+02:00 Bob Arning <span dir="ltr"><<a href="mailto:arning315@comcast.net" target="_blank">arning315@comcast.net</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div text="#000000" bgcolor="#FFFFFF">
    <p><font face="Georgia">Other that the WeakValueDictionary, is there
        anything referencing either the class or metaclass? If not, then
        they would be collected. Perhaps VW is saving a reference
        elsewhere?</font><br>
    </p><div><div class="h5">
    <br>
    <div class="m_-6117267556797383343moz-cite-prefix">On 3/26/18 3:07 PM, Nicolas Cellier
      wrote:<br>
    </div>
    <blockquote type="cite">
      <div dir="ltr">
        <div>
          <div>
            <div>
              <div>
                <div>
                  <div>
                    <div>Hi all,<br>
                    </div>
                    while porting MatFileReader to Squeak, I encountered
                    this questionable behaviour:<br>
                    <br>
                  </div>
                  I create classes on the fly for holding Matlab
                  struct().<br>
                </div>
                I want those classes to be garbage collected when no
                more used.<br>
                <br>
              </div>
              If I store the classes in a WeakArray, then this should
              normally not work:<br>
            </div>
            I mean that the classes should not be reclaimed.<br>
          </div>
          Why?<br>
          Because the metaclass points strongly to the class (via
          thisClass variable).<br>
        </div>
        <div>And the metaclass can't be reclaimed either, because it
          still has an instance.<br>
        </div>
        <div>That's what happens in Visualworks, classes put in a
          WeakArray are never reclaimed.<br>
        </div>
        <div>Fortunately, there is some Ephemeron support in VW to work
          around this (such loop are detected by ephemerons, and strong
          references by some object ephemerely pointed to does not
          count).<br>
          <br>
        </div>
        <div>In Squeak, I used a WeakValueDictionary, and to my
          surprise, discovered that the classes were reclaimed.<br>
        </div>
        <div>Is this a bug?<br>
          <br>
        </div>
        <div>Nicolas<br>
        </div>
        <div><br>
          <br>
        </div>
      </div>
      <br>
      <fieldset class="m_-6117267556797383343mimeAttachmentHeader"></fieldset>
      <br>
      <pre></pre>
    </blockquote>
    <br>
  </div></div></div>

<br><br>
<br></blockquote></div><br></div>