Re: Weak References
Resending this as a new message because of my apparently broken WYSIWYDG (what you see is what you don't get) email client... :-( Hopefully this doesn't itself get corrupted... > My original email featured a use case, although I had to rewrite it to use > explicit garbage collection. I need a way to watch for when the object is > garbage collected. > > ```js > function rotateStream(interval, format) { > const res = {} > setStream(res, format) > > let stream = new WeakReference(res) , format) > > let timer = setInterval(() => { > setStream(stream.get(), format) > }, interval) > > stream.onCollect(() => { > // reference is dead, clear this timer > clearTimer(timer) > // break local strong references > timer = stream = null > }) > > // Return a strong reference > return res > } > ``` > > The complicating part is that I also need the interval callback to weakly > reference the value. Otherwise, I've always got one active strong reference > to the stream. This is with a listener for when the value is garbage > collected. > > Here's the version without a listener (it's simpler, but more crude): > > ```js > function rotateStream(interval, format) { > const res = {} > setStream(res, format) > > let stream = new WeakReference(res) > > setInterval(function () { > try { > setStream(stream.get(), format) > } catch (e) { > // reference is dead, clear this timer > clearInterval(this) > > // break strong reference to weak one > stream = null > } > }, interval) > > // Return a strong reference > return res > } > ``` > > Sorry for the ambiguity. > > (edited out a few bugs from the original) (it got corrupted when saving somehow :-( ) On Sun, Sep 6, 2015 at 11:34 PM, Mark S. Millerwrote: > > > On Sun, Sep 6, 2015 at 8:04 PM, Isiah Meadows > wrote: >> >> My original email featured a use case, although I had to rewrite it to use >> explicit garbage collection. I need a way to watch for when the object is >> garbage collected. >> >> ```js > > [...Somehow reformatted in transit...] > >> >> ``` > > > Hi Isiah, the text in the above code block somehow got reformatted in > transit. You can see the problem at > https://mail.mozilla.org/pipermail/es-discuss/2015-September/044141.html > https://esdiscuss.org/topic/weak-references#content-10 > > Since the code below does not have the callback, I don't yet have enough > context to understand your message. When the GC notifies that the stream is > to be collected, by notifying some callback, why does that callback need to > access the stream? What does the callback do with the stream during the > notification that the stream is condemned? > > Is the interval callback somehow related to the GC notification callback? As > you see, I just don't get it yet. >> >> >> The complicating part is that I also need the interval callback to weakly >> reference the value. Otherwise, I've always got one active strong reference >> to the stream. This is with a listener for when the value is garbage >> collected. >> >> Here's the version without a listener (it's simpler, but more crude): >> >> ```js >> function rotateStream(interval, format) { >> const res = {} >> setStream(res, format) >> >> let stream = new WeakReference(res) >> >> setInterval(function () { >> try { >> setStream(stream.get(), format) >> } catch (e) { >> // reference is dead, clear this timer >> clearInterval(this) >> >> // break strong reference to weak one >> stream = null >> } >> }, interval) >> >> // Return a strong reference >> return res >> } >> ``` >> >> Sorry for the ambiguity. >> >> (edited out a few bugs from the original) >> >> > > -- > Cheers, > --MarkM -- Isiah Meadows ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Weak References
On Mon, Sep 7, 2015 at 10:19 AM, Isiah Meadowswrote: > Resending this as a new message because of my apparently broken > WYSIWYDG (what you see is what you don't get) email client... :-( > > Hopefully this doesn't itself get corrupted... > > > My original email featured a use case, although I had to rewrite it to > use explicit garbage collection. I need a way to watch for when the object > is garbage collected. > > > > ```js > > function rotateStream(interval, format) { > > const res = {} > > setStream(res, format) > > > > let stream = new WeakReference(res) , format) > Unbalanced parens. > > > > let timer = setInterval(() => { > > setStream(stream.get(), format) > > }, interval) > > > > stream.onCollect(() => { > > // reference is dead, clear this timer > > clearTimer(timer) > > // break local strong references > > timer = stream = null > > }) > > > > // Return a strong reference > > return res > > } > > ``` > > > > The complicating part is that I also need the interval callback to > weakly reference the value. Otherwise, I've always got one active strong > reference to the stream. This is with a listener for when the value is > garbage collected. > > > > Here's the version without a listener (it's simpler, but more crude): > > > > ```js > > function rotateStream(interval, format) { > > const res = {} > > setStream(res, format) > > > > let stream = new WeakReference(res) > > > > setInterval(function () { > > try { > > setStream(stream.get(), format) > > } catch (e) { > > // reference is dead, clear this timer > > clearInterval(this) > > > > // break strong reference to weak one > > stream = null > > } > > }, interval) > > > > // Return a strong reference > > return res > > } > > ``` > > > > Sorry for the ambiguity. > > > > (edited out a few bugs from the original) > > (it got corrupted when saving somehow :-( ) > > > On Sun, Sep 6, 2015 at 11:34 PM, Mark S. Miller > wrote: > > > > > > On Sun, Sep 6, 2015 at 8:04 PM, Isiah Meadows > > wrote: > >> > >> My original email featured a use case, although I had to rewrite it to > use > >> explicit garbage collection. I need a way to watch for when the object > is > >> garbage collected. > >> > >> ```js > > > > [...Somehow reformatted in transit...] > > > >> > >> ``` > > > > > > Hi Isiah, the text in the above code block somehow got reformatted in > > transit. You can see the problem at > > https://mail.mozilla.org/pipermail/es-discuss/2015-September/044141.html > > https://esdiscuss.org/topic/weak-references#content-10 > > > > Since the code below does not have the callback, I don't yet have enough > > context to understand your message. When the GC notifies that the stream > is > > to be collected, by notifying some callback, why does that callback need > to > > access the stream? What does the callback do with the stream during the > > notification that the stream is condemned? > > > > Is the interval callback somehow related to the GC notification > callback? As > > you see, I just don't get it yet. > >> > >> > >> The complicating part is that I also need the interval callback to > weakly > >> reference the value. Otherwise, I've always got one active strong > reference > >> to the stream. This is with a listener for when the value is garbage > >> collected. > >> > >> Here's the version without a listener (it's simpler, but more crude): > >> > >> ```js > >> function rotateStream(interval, format) { > >> const res = {} > >> setStream(res, format) > >> > >> let stream = new WeakReference(res) > >> > >> setInterval(function () { > >> try { > >> setStream(stream.get(), format) > >> } catch (e) { > >> // reference is dead, clear this timer > >> clearInterval(this) > >> > >> // break strong reference to weak one > >> stream = null > >> } > >> }, interval) > >> > >> // Return a strong reference > >> return res > >> } > >> ``` > >> > >> Sorry for the ambiguity. > >> > >> (edited out a few bugs from the original) > >> > >> > > > > -- > > Cheers, > > --MarkM > > > > -- > Isiah Meadows > -- Cheers, --MarkM ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Weak References
I also don't get the purpose of this code. What is it trying to achieve? ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Weak References
It's replacing the prototype of a stream periodically to point to a new writable file output stream pointing to a new file, but I want to kill the interval timer when the object is garbage collected. Something like this: 1. Create new stream. 2. Point it to a log file. 3. Every tick on a given interval, do this: 3.1. Change the destination of the stream to a new log file. 4. When the stream itself is GC'd, do this: 4.1. Clear the timer. 4.2. Clear all local references to both the timer and the stream. 5. Return the stream. The catch is that I don't want the stream strongly referenced in any local closures because it would end up never being collected - there would always be an active strong reference to it. I don't think a `Reflect.onOnlyOneReferenceLeft(obj, callback)` would make it into the language, so weak references are the best way to avoid this. And if I were doing this in Java or C++, I would most definitely use weak references/pointers for this use case. (This is out of the land of what automatic garbage collection can handle at this point.) On Mon, Sep 7, 2015, 13:33 Mark S. Millerwrote: > I also don't get the purpose of this code. What is it trying to achieve? > > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Weak References
function rotateStreamHelper(weakRes, interval, format) { let timer = setInterval(() => setStream(weakRes.get(), format), interval); weakRes.register(() => clearInterval(timer)); } function rotateStream(interval, format) { const res = {}; setStream(res, format); rotateStreamHelper(new WeakRef(res), interval, format); // Return a strong reference return res; } Given http://wiki.ecmascript.org/doku.php?id=strawman:weak_references does this implement your intention? On Mon, Sep 7, 2015 at 10:53 AM, Isiah Meadowswrote: > It's replacing the prototype of a stream periodically to point to a new > writable file output stream pointing to a new file, but I want to kill the > interval timer when the object is garbage collected. > > Something like this: > > 1. Create new stream. > 2. Point it to a log file. > 3. Every tick on a given interval, do this: > 3.1. Change the destination of the stream to a new log file. > 4. When the stream itself is GC'd, do this: > 4.1. Clear the timer. > 4.2. Clear all local references to both the timer and the stream. > 5. Return the stream. > > The catch is that I don't want the stream strongly referenced in any local > closures because it would end up never being collected - there would always > be an active strong reference to it. I don't think a > `Reflect.onOnlyOneReferenceLeft(obj, callback)` would make it into the > language, so weak references are the best way to avoid this. And if I were > doing this in Java or C++, I would most definitely use weak > references/pointers for this use case. (This is out of the land of what > automatic garbage collection can handle at this point.) > > On Mon, Sep 7, 2015, 13:33 Mark S. Miller wrote: > >> I also don't get the purpose of this code. What is it trying to achieve? >> >> -- Cheers, --MarkM ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Weak References
Yes. On Mon, Sep 7, 2015, 14:30 Mark S. Millerwrote: > function rotateStreamHelper(weakRes, interval, format) { > let timer = setInterval(() => setStream(weakRes.get(), format), > interval); > weakRes.register(() => clearInterval(timer)); > } > > function rotateStream(interval, format) { > const res = {}; > setStream(res, format); > > rotateStreamHelper(new WeakRef(res), interval, format); > > // Return a strong reference > return res; > } > > > Given > http://wiki.ecmascript.org/doku.php?id=strawman:weak_references > does this implement your intention? > > > > On Mon, Sep 7, 2015 at 10:53 AM, Isiah Meadows > wrote: > >> It's replacing the prototype of a stream periodically to point to a new >> writable file output stream pointing to a new file, but I want to kill the >> interval timer when the object is garbage collected. >> >> Something like this: >> >> 1. Create new stream. >> 2. Point it to a log file. >> 3. Every tick on a given interval, do this: >> 3.1. Change the destination of the stream to a new log file. >> 4. When the stream itself is GC'd, do this: >> 4.1. Clear the timer. >> 4.2. Clear all local references to both the timer and the stream. >> 5. Return the stream. >> >> The catch is that I don't want the stream strongly referenced in any >> local closures because it would end up never being collected - there would >> always be an active strong reference to it. I don't think a >> `Reflect.onOnlyOneReferenceLeft(obj, callback)` would make it into the >> language, so weak references are the best way to avoid this. And if I were >> doing this in Java or C++, I would most definitely use weak >> references/pointers for this use case. (This is out of the land of what >> automatic garbage collection can handle at this point.) >> >> On Mon, Sep 7, 2015, 13:33 Mark S. Miller wrote: >> >>> I also don't get the purpose of this code. What is it trying to achieve? >>> >>> > > > -- > Cheers, > --MarkM > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss