[squeak-dev] Suspiscious "weak" value reclaimed

Nicolas Cellier nicolas.cellier.aka.nice at gmail.com
Tue Mar 27 10:33:44 UTC 2018


Hi Bob,
yes, you force me to rethink. The way mark and sweep GC works, cycles in
object graphs do not matter.
As long as the objects in the cycles are only pointed to weakly from roots,

| value weak array2 |
value := Array new.
weak := WeakArray with: value.
3 timesRepeat: [Smalltalk garbageCollect].
self assert: (weak count: #notNil) = 1. "Not yet reclaimed, thisContext
points to it thru value variable"
value := nil.
3 timesRepeat: [Smalltalk garbageCollect].
self assert: (weak count: #notNil) = 0. "This time, it is reclaimed"

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

It's then VW that behaves strangely: I did not find any pointer to the
class nor its metaclass apart themselves
(I took care to not add the metaclass to metaclass superclass subclasses,
because those are not weak).
This has obscured my mind.
Sorry for the noise.

2018-03-26 23:15 GMT+02:00 Bob Arning <arning315 at comcast.net>:

> 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?
>
> On 3/26/18 3:07 PM, Nicolas Cellier wrote:
>
> Hi all,
> while porting MatFileReader to Squeak, I encountered this questionable
> behaviour:
>
> I create classes on the fly for holding Matlab struct().
> I want those classes to be garbage collected when no more used.
>
> If I store the classes in a WeakArray, then this should normally not work:
> I mean that the classes should not be reclaimed.
> Why?
> Because the metaclass points strongly to the class (via thisClass
> variable).
> And the metaclass can't be reclaimed either, because it still has an
> instance.
> That's what happens in Visualworks, classes put in a WeakArray are never
> reclaimed.
> 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).
>
> In Squeak, I used a WeakValueDictionary, and to my surprise, discovered
> that the classes were reclaimed.
> Is this a bug?
>
> Nicolas
>
>
>
>
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20180327/68d9c5ac/attachment.html>


More information about the Squeak-dev mailing list